How to Create a Custom Artisan Command in Laravel

How to Create a Custom Artisan Command in Laravel

posted 2 min read

Laravel's Artisan CLI is a powerful tool that streamlines various development tasks. While it offers numerous built-in commands, there are times when you might need a custom command tailored to your application's specific needs. Let's walk through creating a custom Artisan command that generates dummy users using Laravel's factory system.

Step 1: Generate the Command

Use Artisan's make:command to scaffold a new command class:

php artisan make:command CreateUsers

This command creates a new file at

app/Console/Commands/CreateUsers.php

Step 2: Define the Command's Signature and Description

Open the newly created CreateUsers.php file and set the command's signature and description:

protected $signature = 'create:users {count=10}';
protected $description = 'Generate dummy users using factories';

{count=10}: An optional argument with a default value of 10.

$description: Provides a brief explanation of the command's purpose.

Step 3: Implement the Command Logic

Within the same file, update the handle method to generate users:

public function handle()
{
    $count = (int) $this->argument('count');

    \App\Models\User::factory($count)->create();

    $this->info("Successfully created {$count} users.");
}

This method retrieves the count argument, uses Laravel's factory system to generate the specified number of users, and outputs a success message.
ItSolutionStuff

Step 4: Run the Command

Execute your custom command using Artisan:

php artisan create:users 5

This command will generate 5 dummy users. If you omit the count argument, it defaults to 10.

Bonus: Adding Options and Prompts

You can enhance your command by adding options and interactive prompts. For instance, to include an --admin option:

protected $signature = 'create:users {count=10} {--admin}';

Then, modify the handle method to assign the admin role based on the option:

public function handle()
{
    $count = (int) $this->argument('count');
    $isAdmin = $this->option('admin');

    \App\Models\User::factory($count)->create([
        'is_admin' => $isAdmin,
    ]);

    $this->info("Successfully created {$count} " . ($isAdmin ? 'admin' : 'regular') . " users.");
}

This enhancement allows you to specify whether the generated users should have admin privileges.

Conclusion

Creating custom Artisan commands in Laravel empowers you to automate repetitive tasks and tailor the CLI to your application's needs. By following this tutorial, you've learned how to generate a command, define its signature, implement its logic, and enhance it with options.

Feel free to expand upon this foundation to create more complex commands that suit your project's requirements!

If you read this far, tweet to the author to show them you care. Tweet a Thanks

Nice and clear walkthrough—really handy for speeding up user seeding during testing! Have you tried combining this with seeder classes or using states for more customized user data?

Thank you.

Haven't tried the later. But I do have some experiences in combining this with seeder classes for more customized user data.

I'm definitely interested in learning more

More Posts

Laravel Middleware: How To Craft Your Own HTTP Gatekeepers.

Darlington Okorie - May 19

Laravel is How Development Should Be

psypher1 - Mar 31

Laravel 12 & Laravel Cloud: The Next Big Leap in Web Development

Snehal Kadwe - Mar 9

Setup a LAMP Server on a VPS in Under an Hour: The Ultimate Guide to Web Hosting

Gift Balogun - Feb 19

create a custom schematic to do things like cleaning up unused imports in a web application

Sunny - Jul 4
chevron_left