Customizing the Laravel Nova artisan nova:user command

Javier Mora
Mar 18 · 2 min read

Laravel Nova admin panel offers a simple way to create the initial admin user to log in into our new admin panel with the simple artisan command:

php artisan nova:user

This command will ask you a name, email and password and will create a new user in our database. But what happens if we have our name and surname as two different fields, or if we need to add a is_admin boolean, or another required speciecial field?

To our luck, Nova has us covered. To change the way users are created with this command simply add this code to your boot method of your NovaServiceProvider :

use Illuminate\Support\Facades\Hash;/**
* Bootstrap any application services.
*
* @return void
*/
public function boot()
{
parent::boot();
Nova::createUserUsing(function($command) { return [
$command->ask('Name'),
$command->ask('Email Address'),
$command->secret('Password'),
// My Custom prompts:
$command->ask('Favorite color'),
];
}, function($name, $email, $password, $color) { (new User)->forceFill([
'name' => $name,
'email' => $email,
'password' => Hash::make($password),
// My custom fields
'email_verified_at' => now(),
'activated_at' => now(),
'is_admin' => true,
'favorite_color' => $color,
])->save();
});
}

The first parameter of the Nova::createUserUsing method is a Closure that takes a Laravel Artisan command instance and returns an array of prompts that should be asked in order. You can use any artisan prompt you want.

The second parameter takes all the values you asked to the user in order and creates your user. Note that is convenient you to use forceFill to bypass the Laravel mass asignment protection.

This is my first Medium post. So, I hope you find it useful.

Welcome to a place where words matter. On Medium, smart voices and original ideas take center stage - with no ads in sight. Watch
Follow all the topics you care about, and we’ll deliver the best stories for you to your homepage and inbox. Explore
Get unlimited access to the best stories on Medium — and support writers while you’re at it. Just $5/month. Upgrade