Created
June 28, 2020 16:40
-
-
Save julienbourdeau/fbb9634c7359985a290c2e543dc0fd34 to your computer and use it in GitHub Desktop.
Laravel Create User Command
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
namespace App\Console\Commands\Dev; | |
use App\User; | |
use Illuminate\Console\Command; | |
use Illuminate\Support\Facades\DB; | |
use Illuminate\Support\Facades\Hash; | |
class CreateAdminUserCommand extends Command | |
{ | |
protected $signature = 'dev:create-admin {name} {--email=} {--pwd=} {--truncate}'; | |
public function handle() | |
{ | |
throw_unless($this->option('email') && $this->option('pwd'), new \ArgumentCountError('Both `email` and `pwd` are required.')); | |
if ($this->option('truncate')) { | |
$this->line('Deleting all existing users...'); | |
DB::table('users')->truncate(); | |
} | |
$user = User::forceCreate([ | |
'name' => $this->argument('name'), | |
'email' => $this->option('email'), | |
'password' => Hash::make($this->option('pwd')), | |
'email_verified_at' => now(), | |
]); | |
$this->info("Created new {$user->name} <{$user->email}> user [ID:{$user->id}]"); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment