You can also create a new console command which can be called from the command line. This is especially useful if you want to create new users on demand.
This example makes use of laravel fortify but you can also use your own user registration logic.
First create a new console command:
php artisan make:command CreateUserCommand
Then add the implementation:
<?php
namespace App\Console\Commands;
use Illuminate\Console\Command;
use App\Actions\Fortify\CreateNewUser;
class CreateUserCommand extends Command
{
/**
* The name and signature of the console command.
*
* @var string
*/
protected $signature = 'user:create {--u|username= : Username of the newly created user.} {--e|email= : E-Mail of the newly created user.}';
/**
* The console command description.
*
* @var string
*/
protected $description = 'Manually creates a new laravel user.';
/**
* Execute the console command.
* https://laravel.com/docs/9.x/artisan
*
* @return int
*/
public function handle()
{
// Enter username, if not present via command line option
$name = $this->option('username');
if ($name === null) {
$name = $this->ask('Please enter your username.');
}
// Enter email, if not present via command line option
$email = $this->option('email');
if ($email === null) {
$email = $this->ask('Please enter your E-Mail.');
}
// Always enter password from userinput for more security.
$password = $this->secret('Please enter a new password.');
$password_confirmation = $this->secret('Please confirm the password');
// Prepare input for the fortify user creation action
$input = [
'name' => $name,
'email' => $email,
'password' => $password,
'password_confirmation' => $password_confirmation
];
try {
// Use fortify to create a new user.
$new_user_action = new CreateNewUser();
$user = $new_user_action->create($input);
}
catch (\Exception $e) {
$this->error($e->getMessage());
return;
}
// Success message
$this->info('User created successfully!');
$this->info('New user id: ' . $user->id);
}
}
You can execute the command via:
php artisan user:create -u myusername -e [email protected]
I recommend to always ask for the password via the user input, not as parameter for security reasons.