How to change password in Laravel?

I have a laravel website where I need to change an existing user password. The website has a password reset system but the user lost access to his email account. So I just want to change the password directly into the database.

  1. Open the command line and go to your Laravel project’s root directory.
  2. Run the php artisan tinker command to enter the tinker environment.
php artisan tinker
  1. Use the App\User model to retrieve the user you want to change the password for.
$user = App\User::where('email', 'user@example.com')->first();
$user->password = Hash::make('password');
$user->save();
  1. Use the $user->password property to set the new password. You can use the Hash::make() function to hash the password before storing it.
  2. To save the new password, you need to call the save() method on the user object.

Note: You can use id instead of email.

$user = App\User::where('id', '1')->first();

Depending on the project, your user model could be App\Models\User

If you don’t have access to the command line, you can create a quick route to change the user password.

Route::get('change-password', function() {
    $user = App\Models\User::where('email', 'user@example.com')->first();
    $user->password = Hash::make('password');
    $user->save();
});

Make sure to use the User model and hashing class

use App\Models\User;
use Illuminate\Support\Facades\Hash;