How to Create Custom Artisan Command in Laravel 9 – Example Tutorial

This tutorial will help you learn how to create a custom artisan command in your Laravel application and use that artisan command to generate test data and user records, to insert them in a database.

Laravel’s command line interface is very helpful to enhance development speed. The valuable and assisting commands allow us to do multiple tasks in short span.

There are originally limited Artisan commands in the framework, but we can make custom artisan command by our self. In this tutorial on Laravel Artisan commands, we will learn how to make a Custom Artisan Command ourselves to use in Laravel.

Initially, we will check a pre-defined list of artisan commands, by writing the command shown below.

php artisan list

Make Custom Artisan Command – Laravel 9

  • Step 1: Download New Laravel App
  • Step 2: Connect Laravel to Database
  • Step 3: Generate Artisan Command
  • Step 4: Create Artisan Command
  • Step 5: Run Artisan Command

Download New Laravel App

We can easily create a new Laravel application, using the composer. Use the composer command given below to create a fresh Laravel project from scratch.

composer create-project laravel/laravel --prefer-dist laravel-demo

Get inside your Laravel application.

cd laravel-demo

Connect Laravel to Database

Next, we need to connect our Laravel application to a database. This can be done by adding a database name, username and password in our .env file as shown below.

DB_CONNECTION=mysql
DB_HOST=localhost
DB_PORT=3306
DB_DATABASE=laravel_db
DB_USERNAME=root
DB_PASSWORD=

In case you are using the MAMP local server in your Mac operating system, you need to add UNIX_SOCKET and DB_SOCKET database credentials in .env file, as shown below.

UNIX_SOCKET=/Applications/MAMP/tmp/mysql/mysql.sock
DB_SOCKET=/Applications/MAMP/tmp/mysql/mysql.sock

Before you generate user data, run the outstanding migrations, and execute the migrate Artisan command.

php artisan migrate

Generate Artisan Command

To create a new command, you need to make use of the make:command Artisan command. This will generate a new command class inside your Laravel application’s app/Console/Commands folder.

Moreover, if the folder does not exists in your Laravel project by default, it will be created once you invoke the make:command Artisan command

So, open your terminal window, type the command and press enter to create a new artisan command.

php artisan make:command generateUserData

Create New Laravel Artisan Command

To build a new Laravel Artisan command, you need to open your app/Console/Commands/generateUserData.php file, and define your command name to the $signature variable.

In this case, generate-users is your command name, and {count} variable refers to the number of the records that will be generated.

The handle() function will hold the logic to run the factory tinker command, manifesting all records in database.

Next, you have to update your app/Console/Commands/generateUserData.php file using the following code.

<?php
namespace App\Console\Commands;
use Illuminate\Console\Command;
use App\Models\User;

class generateUserData extends Command
{
    /**
     * The name and signature of the console command.
     *
     * @var string
     */
    protected $signature = 'create:generate-users {count}';
    /**
     * The console command description.
     *
     * @var string
     */
    protected $description = 'Creates test user data and insert into the database.';
    /**
     * Create a new command instance.
     *
     * @return void
     */
    public function __construct()
    {
        parent::__construct();
    }
    /**
     * Execute the console command.
     *
     * @return int
     */
    public function handle()
    {
        $usersData = $this->argument('count');
        for ($i = 0; $i < $usersData; $i++) { 
            User::factory()->create();
        }          
    }
}

Run Custom Artisan Command

Finally, type your custom artisan command on the console screen and execute it to generate users’ data in the database.

You can change the total number of records as per your choice whereas the command stays the same.

php artisan create:generate-users 250
Laravel Create Custom Artisan Command Tutorial

Conclusion

This tutorial is of great help to ease your application development in Laravel. We have learned how to build Custom Artisan Commands.

This guide helps us how we can create and keep our custom commands in the app/Console/Commands folder of our Laravel app.

So, in this eloquent guide, we have learnt how to write Custom Laravel Artisan Commands from scratch, and also to create dummy records and insert them into the database.

Leave a Comment