Skip to content

Instantly share code, notes, and snippets.

@meigwilym
Last active March 5, 2024 02:20
Show Gist options
  • Star 20 You must be signed in to star a gist
  • Fork 3 You must be signed in to fork a gist
  • Save meigwilym/d8ac0b724b4ef9c20ed7768c92838d92 to your computer and use it in GitHub Desktop.
Save meigwilym/d8ac0b724b4ef9c20ed7768c92838d92 to your computer and use it in GitHub Desktop.
Laravel Create User Command
<?php
// routes/console.php
// quickly create an user via the command line
Artisan::command('user:create', function () {
$name = $this->ask('Name?');
$email = $this->ask('Email?');
$pwd = $this->ask('Password?');
// $pwd = $this->secret('Password?'); // or use secret() to hide the password being inputted
\DB::table('users')->insert([
'name' => $name,
'email' => $email,
'password' => bcrypt($pwd),
'created_at' => date_create()->format('Y-m-d H:i:s'),
'updated_at' => date_create()->format('Y-m-d H:i:s'),
]);
$this->info('Account created for '.$name);
});
@bretterer
Copy link

bretterer commented Mar 5, 2024

Updated to use laravel/prompts

<?php

namespace App\Console\Commands;

use App\Models\User;
use Illuminate\Console\Command;
use Illuminate\Support\Facades\Hash;
use function Laravel\Prompts\confirm;
use function Laravel\Prompts\info;
use function Laravel\Prompts\password;
use function Laravel\Prompts\select;
use function Laravel\Prompts\text;

class CreateUser extends Command
{
    /**
     * The name and signature of the console command.
     *
     * @var string
     */
    protected $signature = 'make:user';

    /**
     * The console command description.
     *
     * @var string
     */
    protected $description = 'Create a new user';

    /**
     * Execute the console command.
     *
     * @return void
     */
    public function handle()
    {
        //$this->rolesMap = Role::all()->pluck('title', 'id')->toArray();

        $details  = $this->askForUserDetails($details ?? null);
        $name     = $details['name'];
        $email    = $details['email'];
        $password = $details['password'];
        $role     = $details['role'] == 'helper' ? false : true;


        confirm("Create user {$name} <{$email}> as {$details['role']}?", true);

        $user = User::forceCreate(['name' => $name, 'email' => $email, 'password' => Hash::make($password), 'is_admin' => $role]);
        //$user->roles()->attach(array_flip($this->rolesMap)[$role]);
        info("Created new user {$user->email}");
    }

	/**
	 * @param null $defaults
	 * @return array
	 */
    protected function askForUserDetails($defaults = null)
    {
        $name = text(
            label: 'What is the users name?',
            placeholder: 'E.g. Brian Retterer',
        );
        $email = text(
            label: 'What is the users email address?',
            placeholder: 'E.g. hello@example.com',
            validate: fn (string $value) => match (true) {
                !filter_var($value, FILTER_VALIDATE_EMAIL) => 'The email is invalid.',
                (User::query()->where('email', $value)->first() != null) => 'The email already exists.',
                default => null
            }
        );
        $password = password(
            label: 'What is the users password?',
            placeholder: 'password',
            hint: 'Minimum 8 characters.',
            required: true,
            validate: fn (string $value) => match (true) {
                strlen($value) < 8 => 'The password must be at least 8 characters.',
                default => null
            }
        );
        $role = select(
            label: 'What role should the user have?',
            options: [
                'helper' => 'Helper',
                'admin' => 'Admin',
            ],
            default: 'helper'
        );

        return compact('name', 'email', 'password', 'role');
    }

	
}```

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment