I had a really simple need where every user should get a random X number of characters password in a Laravel application. I was sure of Google having a simple one-liner code result and that would make my day, but none.


So the next thing I did was checking out Laravel documentation and I came across str_random($length) and Hash::make($password)


Let's say we want to generate 12-character length password. So here is simple code to achieve it:


$hashedRandomPassword = Hash::make(str_random(12));


Do not forget to add the Hash facade in order to use make method from it:

use Illuminate\Support\Facades\Hash;

Or, you can also use bcrypt()

$hashedRandomPassword = bcrypt(str_random(12));


That's it for today. Hope you liked this small tip for generating random passwords in Laravel.
Happy Coding! :)