xxxxxxxxxx
//select random value from array
use Illuminate\Support\Arr;
$array = [1, 2, 3, 4, 5];
$random = Arr::random($array);
//generate random string of specific length
use Illuminate\Support\Str;
$random = Str::random(40);
xxxxxxxxxx
use Illuminate\Support\Str;
// Generate a random string with default length (16)
$randomString = Str::random();
// Generate a random string with a specific length (8)
$randomString = Str::random(8);
xxxxxxxxxx
function generateRandomString(int $n=0)
{
$al = ['a','b','c','d','e','f','g','h','i','j','k'
, 'l', 'm', 'n', 'o', 'p', 'q', 'r', 's', 't', 'u',
'v', 'w', 'x', 'y', 'z', 'A', 'B', 'C', 'D', 'E',
'F','G','H','I','J','K', 'L', 'M', 'N', 'O', 'P',
'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X', 'Y', 'Z',
'0', '2', '3', '4', '5', '6', '7', '8', '9'];
$len = !$n ? random_int(7, 12) : $n; // Chose length randomly in 7 to 12
$ddd = array_map(function($a) use ($al){
$key = random_int(0, 60);
return $al[$key];
}, array_fill(0,$len,0));
return implode('', $ddd);
}