Skip to content

Instantly share code, notes, and snippets.

@julienbourdeau
Created June 28, 2020 16:40
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save julienbourdeau/fbb9634c7359985a290c2e543dc0fd34 to your computer and use it in GitHub Desktop.
Save julienbourdeau/fbb9634c7359985a290c2e543dc0fd34 to your computer and use it in GitHub Desktop.
Laravel Create User Command
<?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