Laravel 9 Create Custom Artisan Command Example Tutorial

Last Updated on by in Laravel

In this tutorial, you will learn how to create a custom artisan command in the Laravel application and use the custom laravel artisan command to generate the test data or user records and insert them into the database.

Laravel comes with Artisan command-line interface, and it is profoundly helpful in enhancing the development speed by providing assistive and valuable commands. The locus of artisan command is usually found at the root of your application.

Although Artisan commands are limited in number, that doesn’t mean you can not make the custom artisan command. In this Laravel Artisan command tutorial, we will explain how to make a custom artisan command in laravel:

You can check out the pre-defined set of artisan commands using the following command.

php artisan list

How to Make Custom Artisan Command in 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

The composer provides handy way to install laravel app, open terminal and execute the suggested command to create a fresh Laravel project from scratch.

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

Get into the laravel app.

cd laravel-demo

Connect Laravel to Database

Next, you have to connect the laravel app to database, it can be done by inserting the database name, username and password in the .env file.

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

If you are using MAMP local server in macOs; make sure to append UNIX_SOCKET and DB_SOCKET below database credentials in .env file.

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

Before generating the user data, you have to run the outstanding migrations, hence execute the migrate Artisan command.

php artisan migrate

Generate Artisan Command

To create a new command, you may use the make:command Artisan command. This command will generate a new command class inside the app/Console/Commands folder.

Fret not if this folder does not exist in your app – it will be created when you invoke the make:command Artisan command.

Open the terminal window, type command and hit 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 have to go to the app/Console/Commands/generateUserData.php file; in this file, you have to define the command name to the $signature variable.

Here generate-users is the command name, and {count} is the variable that refers to the number of the records to be generated.

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

After that you need to update app/Console/Commands/generateUserData.php file with 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

In the final step, we have to type the custom artisan command on the console screen and execute it to generate the users’ data into the database.

You may change the total number of the records as per your choice whereas the command remains the same.

php artisan create:generate-users 250

Laravel Create Custom Artisan Command Tutorial

Conclusion

Apart from ready-made artisan commands, we learned how to build our custom Artisan commands.

Throughout this guide, we saw commands are ideally kept in the app/Console/Commands folder; nevertheless, we are free to select our own storage location.

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