how to Create Custom Artisan Command in LARAVEL

Spread the love

Hello Guys,

In this article, we’ll dive into the world of crafting a custom artisan command for Laravel 9. We’ll showcase some impressive examples of how to create custom artisan commands and provide a concise overview of this process in the context of Laravel 9. This tutorial will guide you through the steps needed to develop a unique artisan command tailored specifically to your project’s requirements.

If you’re interested in developing your own artisan commands, whether it’s for tasks like setting up a project or managing admin users within a Laravel application, we’ll walk you through the essential steps. Laravel comes equipped with a range of artisan commands for various tasks, such as generating migrations, models, and controllers.

In this particular case, we’ll harness Laravel’s artisan command capabilities to fashion a custom “php artisan create:users” command. This command will seamlessly accept an integer parameter. Subsequently, we’ll demonstrate how to employ a factory to generate users based on the provided command argument.

Let’s embark on the journey of crafting your very own artisan command within your Laravel application by following the step-by-step instructions below, all centered around the “create custom artisan command” keyword.

Step 1: Install Laravel

To begin, launch your terminal or command prompt and enter the following command to obtain a new and up-to-date Laravel application version:

Also Read: How to Use LARAVEL DB Raw Query?

composer create-project laravel/laravel example-app

Step 2: Database Configuration

In this step, it’s crucial to include the necessary database settings in the .env file. Once you’ve completed this task, proceed by executing the migration command following the inclusion of the provided information. This ensures a seamless setup and configuration process.


Proceed by executing the migration command to generate a ‘users‘ table.

Also Read: How To Send Email In Laravel 9

php artisan migrate

Step 3: Create Custom Artisan Command

To create the “CreateUsers” class in this stage, you should utilize the following command. Afterward, simply paste the provided code below into it. This command will be referred to as “create-users.”

php artisan make:command CreateUsers

Now, let’s proceed with the necessary command file update.

namespace App\Console\Commands;
use Illuminate\Console\Command;
use App\Models\User;
class CreateUsers extends Command
     * The name and signature of the console command.
     * @var string
    protected $signature = 'create:users {count}';
     * The console command description.
     * @var string
    protected $description = 'Create Dummy Users for your App';
     * Execute the console command.
     * @return int
    public function handle()
        $numberOfUsers = $this->argument('count');
        for ($i = 0; $i < $numberOfUsers; $i++) { 
        return 0;

Step 4: Use Created Artisan Command

In this stage, we’ll execute our tailored command and verify the artisan command by utilizing the “php artisan list” directive.

Also Read: How To Generate PDF Files using DomPDF In Laravel 9

To efficiently generate multiple user profiles, you can execute the following customized command:

php artisan create:users 10
php artisan create:users 5

Ensure to review your users’ table to generate new records within it.

Additionally, don’t forget to inspect your custom command within the provided list.

Also Read: How To Create Repeater Field Using Bootstrap

php artisan list

Embrace the elite realm of Laravel insights – your compass to the best Laravel blog awaits.

Leave a Comment