Laravel Str::random() function Example

10-Apr-2023

.

Admin

Hi Guys,

In this blog, I will show how to generate a random string of the specify length in laravel. I want to learn you str random method in laravel. we will talk about laravel string random method. This tutorial will give you how to generate random string of the specified length.

The Str::random method generates a random string of the specified length. This function uses PHP's random_bytes function. If you want to generate random string if you specify the length in laravel then you can use bellow example.

Example


<?php

namespace App\Http\Controllers;

use Illuminate\Http\Request;

use Illuminate\Support\Str;

class HomeController extends Controller

{

/**

* Show the application dashboard.

*

* @return \Illuminate\Contracts\Support\Renderable

*/

public function index()

{

$random1 = Str::random(8);

$random2 = Str::random(6);

$random3 = Str::random(40);

echo 'Random String 1 : '.$random1;

echo '<br>';

echo 'Random String 2 : '.$random2;

echo '<br>';

echo 'Random String 3 : '.$random3;

echo '<br>';

}

}

It will help you....

#Laravel 8

#Laravel 7

#Laravel

#Laravel 6