This is a really really short one. I just needed to generate a random password for a user and noticed that Google doesn’t really give a one-line answer, which is really simple.
Firstly, how to generate random string – Laravel has a helper called str_random($length). In terms of saving hashed password to database, we use Hash::make($password).
So the end result for generating 8-symbol length password looks as simple as this:
$hashed_random_password = Hash::make(str_random(8));
Of course, don’t forget:
use Illuminate\Support\Facades\Hash;
Generated password can look like:
JnR390XB 2I2so1Wr oVNBAic9
And hash is something like:
$2y$10$E1.y.jjmr7ecmMAiwFFQMO6KXn0scLB2hvVBbx8cny.lREZ6xdDMW $2y$10$pVOI0me9sLPRbyQnxNQrDurSoJScsE6nvu6VVOsMDF8qusH7HUlo2 $2y$10$xVPUPgm1vpUTpZ8jAJE/Xeh7j8G8EFnlQYp27Zjy8qnabqessSc2i
That’s it for now, hope it’s a useful short tip.
bcrypt
Yes, it’s bcrypt under the hood. But you don’t need to know that to use this method I’ve shown.
He meant that you can use the bcrypt helper function:
bcrypt(str_random(6));
This does the trick too: bcrypt(uniqid());
I really don’t understand where to input tge Code, do you mead the user model or registration controller.?.
It totally depends on your logic how/where you want to use it.
The instruction above is suitable for those who are already good in coding but is not a clear instruction for beginners. Beginner doesn’t understand where to put the code, where to start and where to end.
Thanks
I agree, but my articles are not suitable for complete beginners. Otherwise I would have to start and end the articles with building a full Laravel project.
Since Laravel 6 str_random() won’t work. See this Stackoverflow for more info: https://stackoverflow.com/questions/58163406/after-upgrading-laravel-from-5-6-to-6-0-call-to-undefined-str-random-function
Ive try this one, and it works.
Just echo the password and change your password via database
Add it on your display controller. Or in anyblade to see the password.