Laravel Change User Password With Php Artisan

Hello Guys,
Today I will inform you about an essential and advanced feature of Laravel. Today We will use Tinker to change the user password.
Why do We need to use the tinker to change passwords? There are many reasons given below.

1. We don't have access to the database.
2. Unable to receive password reset mail.
3. We can't change passwords with MD5, unlike WordPress. Laravel uses Hash encryption 

So, If we have access to the server/SSH. Then Tinker is the easiest way to change the password. 

About Tinker

Laravel Tinker is a powerful REPL for the Laravel framework, powered by the PsySH package.

Tinker Installation 

All Laravel applications include Tinker by default. However, you may install Tinker using Composer if you have previously removed it from your application:

composer require laravel/tinker

Let's start,

If you have been working with Laravel for a while, you've most probably used thinker to do some testing directly from your terminal.

To get started, run the following command:

php artisan tinker

 You will see this output, which may differ depending on the PHP version.

Psy Shell v0.9.9 (PHP 7.4.14 — cli) by Justin Hileman

After that, We can write a laravel standard query to select the user. which we want to change the password. Let's suppose, we have the email of the user. then we can write this query, 

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


Note: User model namespace may differ depends on laravel version.

Once you run the query, we will see this output.

 App\User {#4649
     id: 1026,
     name: "Template Bench",
     email: "",
     email_verified_at: null,
     #password: "$2y$10$rJn9vaj/SFeYIKV3FwGwu.edPNNqAyUVNKk3dEAuek41LlpP0spDq",
     photo: null,
     role: "user",
     provider: null,
     provider_id: null,
     status: "active",
     #remember_token: "ejmefmLqD1kACL6bw72PngFyl5jNX8o1cWRJ6bMI4i1tcd7weD5AZnTX88Mq",
     created_at: "2021-10-06 02:10:42",
     updated_at: "2022-09-30 11:26:42",

 With this, we will get the user in the $user variable. by using this variable we can change the password property of the user.

$user->password = Hash::make('newpassword');

Once you run this, you will see a hashed string value. Then you can save the user with the new password by running the following command.


Output : 


Finally, your password is changed now. you can log in using the new password. 
If you don't want to use the terminal then you can also change the password using the controller and route. I will create a separate tutorial for that.

For now, Thanks for visiting my post. Please share the post with your friends.