Matthew Daly's Blog

I'm a web developer in Norfolk. This is my blog...

8th January 2018 12:52 pm

Creating An Artisan Task to Set Up a User Account

When working with any Laravel application that implements authentication, you’ll need to set up a user account to be able to work with it. One way of doing that is to add a user in a seeder, but that’s only really suitable if every user is going to use the same details.

Instead, you may want to create an Artisan command to set up the user account. Here’s an example of a command that does that:

namespace App\Console\Commands;
use Illuminate\Console\Command;
use Hash;
class CreateUser extends Command
* The name and signature of the console command.
* @var string
protected $signature = 'create:user';
* The console command description.
* @var string
protected $description = 'Creates a single user';
* Create a new command instance.
* @return void
public function __construct()
* Execute the console command.
* @return mixed
public function handle()
// Get user model from config
$model = config('auth.providers.users.model');
// Let user know what this will do
$this->info('I\'ll ask you for the details I need to set up the user');
// Get username
$name = $this->ask('Please provide the username');
// Get email
$email = $this->ask('Please provide the email address');
// Get password
$password = $this->secret('Please provide the password');
// Create model
$user = new $model;
$user->name = $name;
$user->email = $email;
$user->password = Hash::make($password);
$this->info('User saved');

We fetch the user model from the config, before asking the user for the data we need. Then we insert it into the database and confirm it to the user.

Then we just need to register the command in App\Console\Kernel.php:

protected $commands = [

And we can run our command with php artisan create:user.

Recent Posts

Forcing SSL in Codeigniter

Logging to the ELK Stack With Laravel

Full-text Search With Mariadb

Building a Letter Classifier in PHP With Tesseract OCR and PHP ML

Console Applications With the Symfony Console Component

About me

I'm a web and mobile app developer based in Norfolk. My skillset includes Python, PHP and Javascript, and I have extensive experience working with CodeIgniter, Laravel, Django, Phonegap and Angular.js.