Generate unique random number in Laravel

1 month ago23k times

Code to generate a unique random number in Laravel.

$characters = 'ABCDEFGHIJKLMNOPQRSTUVWXYZ';


// generate a pin based on 2 * 7 digits + a random character
$pin = mt_rand(1000000, 9999999)
    . mt_rand(1000000, 9999999)
    . $characters[rand(0, strlen($characters) - 1)];


// shuffle the result
$string = str_shuffle($pin);


return $string;

Add Answer

Add a codeAdd Code
Remove adsremove