Skip to content

Instantly share code, notes, and snippets.

@dwaard
Created May 20, 2020 07:58
Show Gist options
  • Save dwaard/f8eda3ff214aa58d1c60a87d481f02ce to your computer and use it in GitHub Desktop.
Save dwaard/f8eda3ff214aa58d1c60a87d481f02ce to your computer and use it in GitHub Desktop.
<?php
namespace App\Console\Commands;
use App\User;
use Illuminate\Console\Command;
use Illuminate\Support\Facades\Hash;
class CreateAdminCommand extends Command
{
/**
* The name and signature of the console command.
*
* @var string
*/
protected $signature = 'admin:create {email}';
/**
* The console command description.
*
* @var string
*/
protected $description = 'Create a new admin user';
/**
* Create a new command instance.
*
* @return void
*/
public function __construct()
{
parent::__construct();
}
/**
* Execute the console command.
*
* @return mixed
*/
public function handle()
{
// email address is taken as an argument
$email = $this->argument('email');
// ask for the username
$name = $this->ask('What is your name?');
// ask for password and confirmation
$password = $this->secret('What is the password?');
$password_confirm = $this->secret('Confirm the password');
if ($password == $password_confirm) {
$this->createUser($email, $name, $password);
} else {
$this->error('Passwords do not match');
}
}
/**
* @param $email
* @param $name
* @param $password
*/
private function createUser($email, $name, $password): void
{
$user = User::create([
'email' => $email,
'name' => $name,
'password' => Hash::make($password)
]);
// Do stuff here to make the user an admin user
$user->save();
$this->info("Admin user $user->name is created");
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment