PHP Classes

File: app/Console/Commands/CreateUser.php

Recommend this page to a friend!
  Classes of Thierry Feuzeu   Jaxon DB Admin   app/Console/Commands/CreateUser.php   Download  
File: app/Console/Commands/CreateUser.php
Role: Class source
Content typex: text/plain
Description: Class source
Class: Jaxon DB Admin
Web application to manage SQL of databases
Author: By
Last change: Update of app/Console/Commands/CreateUser.php
Date: 2 months ago
Size: 2,568 bytes
 

Contents

Class file image Download
<?php

namespace App\Console\Commands;

use
App\Models\User;
use
Illuminate\Console\Command;
use
Illuminate\Support\Facades\Hash;
use
Illuminate\Support\Facades\Validator;
use
Illuminate\Validation\Rule;
use
App\Actions\Fortify\PasswordValidationRules;

use function
Laravel\Prompts\password;
use function
Laravel\Prompts\text;

class
CreateUser extends Command
{
    use
PasswordValidationRules;

   
/**
     * The name and signature of the console command.
     *
     * @var string
     */
   
protected $signature = 'user:create
                            {--name= : The new user name}
                            {--email= : The new user email}'
;

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

   
/**
     * Execute the console command.
     */
   
public function handle()
    {
       
$name = $this->option('name');
        if (
$name === null) {
           
$name = text(
               
label: 'Enter the new user name',
               
required: true
           
);
        }
       
$email = $this->option('email');
        if (
$email === null) {
           
$email = text(
               
label: 'Enter the new user email',
               
required: true
           
);
        }
       
$password = password(
           
label: 'Enter the new user password',
           
required: true
       
);
       
$passwordConfirmation = password(
           
label: 'Confirm the new user password',
           
required: true
       
);

       
$validator = Validator::make([
           
'name' => $name,
           
'email' => $email,
           
'password' => $password,
           
'password_confirmation' => $passwordConfirmation,
        ], [
           
'name' => ['required', 'string', 'max:255'],
           
'email' => [
               
'required',
               
'string',
               
'email',
               
'max:255',
               
Rule::unique(User::class),
            ],
           
'password' => $this->passwordRules(),
        ]);
        if (
$validator->fails()) {
           
$errors = $validator->errors();
            foreach (
$errors->all() as $message) {
               
$this->error($message);
            }
            return;
        }

       
$validated = $validator->validated();
       
User::create([
           
'name' => $validated['name'],
           
'email' => $validated['email'],
           
'password' => Hash::make($validated['password']),
        ]);

       
$this->info('User account successfully created!');
    }
}