Skip to content

Instantly share code, notes, and snippets.

@howdu
Forked from ZhangYiJiang/ResetPassword.php
Last active June 6, 2021 14:01
Show Gist options
  • Save howdu/f19eb2b7b52c8aab7df810a342205656 to your computer and use it in GitHub Desktop.
Save howdu/f19eb2b7b52c8aab7df810a342205656 to your computer and use it in GitHub Desktop.
Reset user password command for Laravel
<?php
namespace App\Console\Commands;
use App\User;
use Illuminate\Console\Command;
use Illuminate\Support\Facades\Hash;
use Illuminate\Support\Str;
class ResetPassword extends Command
{
/**
* The name and signature of the console command.
*
* @var string
*/
protected $signature = 'user:reset {email}';
/**
* The console command description.
*
* @var string
*/
protected $description = "Reset a user's password";
/**
* Execute the console command.
*
* @return mixed
*/
public function handle()
{
$email = $this->argument('email');
$user = User::where(compact('email'))->first();
if (empty($user)) {
$this->error("User '$email' not found");
return;
}
if ($this->confirm('Let system generate password for you?')) {
$password = Str::random(16);
$this->info("Your password: $password");
} else {
$password = $this->secret('Please enter your new password');
}
$user->password = Hash::make($password);
$user->save();
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment