How can I update user password in Laravel 5?

Viewed 700

I use Laravel 5.7.

I'm trying to update one my users password

I ran this php artisan tinker

Then

>>> bcrypt(12345);                                                                                                
=> "$2y$10$5woTm5/1w.euUliNCujmMu.oYiC.U8YnRpBHVQN/CxyKXAYB.pGiS"    

and also tried

>>> Hash::make('12345');                                                                               
=> "$2y$10$vjCcADglqpHiLI9tTVlJ2OoaaoQq/qqntRaIaEclTh1exq.vdZcxK" 

I copied the hash update that into my database

I tried to login with 12345. I can't log in.

What did I forget to do ?

3 Answers

Try this :

$password = 'something';
$user = User::findOrFail($userId);
$user->fill(['password' => Hash::make($password)])->save();

Don't forget to use App\User;

You can achieve this in more than one way. I will share 2 ways.

Let say you want to update the password to 12345


option1 : php artisan tinker

If you have access to local set up

In root of your project, type php artisan tinker

When you in the shell mode, type bcrypt('12345') you should get the hash password.

Psy Shell v0.9.9 (PHP 7.1.4 — cli) by Justin Hileman                                            
>>> bcrypt('12345')                                                                              
=> "$2y$10$9ruOL7x2T2Utejl96Mp2MOh2xQB/I2R/R0SMp3G55LokeoWGjpuR2"                                
>>>`

option2 : site

If you don't access to local set up, you can generate a hash via this site

http://www.passwordtool.hu/php5-password-hash-generator

enter image description here

When you have the new hash, copy it.

  • Connect to your database
  • open up users table
  • Go to the row of user that you want to update.
  • Paste in that new hashed password
  • apply or save
  • your new password should take over now
  • Done ✅

Try this:

$user = App\User::where('email', 'user@example.com')->first();
$user->password = Hash::make('password');
$user->save();

This will create a hash password and save it to the user, provided you are able to fetch the user using the email address.

You can also update the user using the id or any other unique identifier that you have for that use. For example, if you have the id, then try this:

$user = App\User::where('id', 101)->first();
$user->password = Hash::make('password');
$user->save();
Related