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.
- Open the command line and go to your Laravel project’s root directory.
- Run the
php artisan tinker
command to enter the tinker environment.
php artisan tinker
- 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();
- Use the
$user->password
property to set the new password. You can use theHash::make()
function to hash the password before storing it. - 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;