In this tutorail, we will learn how to generate a random string of the specify length in laravel. We will talk about laravel string random method.
This tutorial will show you how to generate random string of the specified length.
The Str::random method generates a random string of the specified length. If you want to generate random string if you specify the length in laravel then you can use this example given below.
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>';
}
}
I hope this example helps you.