How to Change User’s Password in Laravel

While working on the Laravel application, you may require to quickly change the password of the user. And you don’t want to follow the boring steps of a forgot password. It’s possible to change the user’s password in Laravel using either a command line or a route.

If you check your Laravel database, it has the users table to store the credentials like email and password. Laravel uses its own Hashing method to encrypt the password. For changing the password you also need to use the same hashing techniques for encryption. It’s mandatory in order to work with Laravel authentication.

Change User’s Password Using Command Line

There are several features Laravel included in its core. Upon installing Laravel, we automatically get all these features. One of them is Laravel Tinker, a REPL powered by the PsySH package. Laravel Tinker allows you to interact with the Laravel database, jobs, events, and more via the command prompt.

We can use Tinker to change the password of the user. All we need to know is the email address of a user. Open the terminal in your project root directory and enter into the Tinker environment using the command:

php artisan tinker
start-tinker

Once you’re inside the Tinker, you’ll have full control over Eloquent ORM. Using Eloquent, you can easily change the user’s password. Let’s say you need to change the password of the user having an email admin@laravel.com.

First, fetch this user by a statement:

$user = App\Models\User::where('email', 'admin@laravel.com')->first();
where-query-tinker

Next, on the user object set the password with the help of the Hash::make() method. For instance, let’s set the password as ‘123456’. So the next statement is:

$user->password = Hash::make('123456');
set-password-tinker

Finally, call the save() method of Eloquent as follows:

$user->save();
save-user-tinker

The above 3 commands will change the password of a specified user. Give it a try and you will see the result.

Change User’s Password in Laravel Using Route

The other way of changing a password is through the route. However, it is not recommended. I am just writing about it as this is also the option to update the password. In the route’s callback function, use the same code we used above in the Tinker.

Let’s declare a route changepassword and pass the code to the callback function as shown below.

Route::get('changepassword', function() {
    $user = App\Models\User::where('email', 'admin@laravel.com')->first();
    $user->password = Hash::make('123456');
    $user->save();
 
    echo 'Password changed successfully.';
});

Now, run the URL YOUR_DOMAIN_URL/changepassword in the browser. It will call the route and change the user’s password. The developer must remove this route once the password is changed.

Related Articles

If you liked this article, then please subscribe to our YouTube Channel for video tutorials.

6 thoughts on “How to Change User’s Password in Laravel

  1. Well, I have tried the first time the tinker method. and I liked well another one is also very nice but we cant use in the regular case because of security reasons right ???

    1. You are right. Changing the password through the route is not recommended. It’s just for quick fixes. Developers should remove the route once they changed the password.

Leave a Reply

Your email address will not be published. Required fields are marked *