Artisan command to reset a user's password programmatically:
php artisan make:command ResetUserPassword
Open the generated command file app/Console/Commands/ResetUserPassword.php
and update the handle method:
use App\Models\User;
use Illuminate\Support\Facades\Hash;
public function handle()
{
$email = $this->ask('Enter the user\'s email');
$password = $this->secret('Enter the new password');
$user = User::where('email', $email)->first();
if ($user) {
$user->password = Hash::make($password);
$user->save();
$this->info('Password reset successfully!');
} else {
$this->error('User not found.');
}
}
After registering the command in Kernel.php
, you can reset a password with:
php artisan reset:user-password