Skip to main content
Tutorial Free

How to generate random password in Laravel

November 29, 2015
1 min read

Tutorial last revisioned on August 11, 2022 with Laravel 9

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.

Enjoyed This Tutorial?

Get access to all premium tutorials, video and text courses, and exclusive Laravel resources. Join our community of 10,000+ developers.

Comments & Discussion

No comments yet…

We'd Love Your Feedback

Tell us what you like or what we can improve

Feel free to share anything you like or dislike about this page or the platform in general.