Skip to content

Instantly share code, notes, and snippets.

@codearachnid
Created January 16, 2025 20:09
Show Gist options
  • Save codearachnid/1a4decd635e967f31b675cd7aaeb6f12 to your computer and use it in GitHub Desktop.
Save codearachnid/1a4decd635e967f31b675cd7aaeb6f12 to your computer and use it in GitHub Desktop.
ResetUserPassword Laravel custom artisan command

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
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment