How to Modify Laravel User Password through CLI / Artisan?

View QuestionsCategory: CodeHow to Modify Laravel User Password through CLI / Artisan?
admin Staff asked 9 months ago
1 Answers
Best Answer
admin Staff answered 9 months ago

Log in through SSH, go to the base / root folder of Laravel. You will see an artisan executable file there. Type this:

php artisan tinker

You get this:

Psy Shell .... by Justin Hileman

Then now type this:

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

BUT what if you get an error:

PHP Error:  Class 'App\User' not found in Psy Shell code on line 1

Exactly as it says the user model is not App\User, it is something else. Every Laravel install may be slightly different so this is normal. To solve, you have to find out the namespace. So go into the folder / file of your laravel install, and find the Model file User.php (likely in the /app/ folder).
Open the User.php
See the “namespace” line in that file. That is the path you need to use in artisan!
Example, if in Users.php it is:

namespace App\Models;

Then line 1 is:

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

You know it will work because it does not throw the not found error above.
Instead it should list out the contents of the User object.

Credit – first part original code was from this: https://medium.com/qunabu-interactive/quick-tip-how-to-change-laravel-user-password-from-command-line-515f55c9d295