Skip to main content

Black Friday 2025! Only until December 1st: coupon FRIDAY25 for 40% off Yearly/Lifetime membership!

Read more here

Create Users: Artisan Command

Premium
10:15

The next point in the client description is about creating users. In fact, it was the very first point in the original job description, but I decided to first work with showing the data and only then with managing it by users. So now it's time.


Task Description from Client

A private (admin) endpoint to create new users. If you want, this could also be an artisan command, as you like. It will mainly be used to generate users for this exercise.


Here's something illogical: if there is only an endpoint to create users, then who/how would create the very first admin user? :)

That's why I vote for the Artisan command from the very beginning, without any API endpoint. So this is precisely what we will do in this lesson.


Generating Artisan Command

This part is easy. We just need to run this in Terminal:

php artisan make:command CreateUserCommand

Notice: I personally like to suffix all Terminal commands with the word "Command" at the end.

We fill in the command signature and description. And then, we need to fill in the handle() method to create the user.

app/Console/Commands/CreateUserCommand.php:

namespace App\Console\Commands;
 
use Illuminate\Console\Command;
 
class CreateUserCommand extends Command
{
protected $signature = 'users:create';
 
protected $description = 'Creates a new user';
 
public function handle()
{
// TODO
}
}

The question is how to ask the Artisan Command user all the data about...

The Full Lesson is Only for Premium Members

Want to access all of our courses? (31 h 16 min)

You also get:

55 courses
Premium tutorials
Access to repositories
Private Discord
Get Premium for $129/year or $29/month

Already a member? Login here

Comments & Discussion

RA
Richard A. Hoyle ✓ Link copied!

Question should we add the time zone data in the public function handle() as well? If the client lives in one time zone and the tours are in another time zone shouldn’t this be apparent to the user?

My reason for wanting to include the time zone in hear; is in Tennessee we have two 2 time zones; Central, and Eastern. Nashville TN is in the Central time zone and Chattanooga TN is in the Eastern time zone they are just 2 hours apart however if you were to leave Nashville going to say the Chattanooga Aquarium that closes at 6:00 pm Eastern time., and you left at say 3:30 to 4:00 Central Time expecting to get there be for they clouded, you may not make it; If you don’t figure in the time difference as well as your driving time. You see the time at the Aquarium will be 7:00 Pm or there abuts.

RA
Richard A. Hoyle ✓ Link copied!

I have a frontend template that has images and video’s, more than one per tour; how can we combined these in hear, so that when the tour data is searched the photos can come up as well. Is this posable using this API?

RA
Richard A. Hoyle ✓ Link copied!

Can we use some thing like this to set the timezone for the user and for the tour?

public function handle() { $user['name'] = $this->ask('Name of the new user'); $user['email'] = $this->ask('Email of the new user'); $user['password'] = $this->secret('Password of the new user');

	$user['timezone'] = $this->choice('timezone of the new user',['timezone','editor'],'America/Chicago');
	 or
 $timezone = $this->choice('timezone of the new user',['timezone','editor'],'America/Chicago');

$roleName = $this->choice('Role of the new user', ['admin', 'editor'], 1);

}

PK
Povilas Korop ✓ Link copied!

Yes, probably, but there will be huge amount of options, then. For the list of the timezones, you may read this course lesson.

RA
Richard A. Hoyle ✓ Link copied!

Wouldn’t it be better to create an admin user, and client user; using table seeder at the very beginning?

PK
Povilas Korop ✓ Link copied!

Maybe. But the client requested differently.