How to Write Custom Laravel Artisan Commands

Before we start how to create custom artisan command, let’s check what is Artisan command and how can we use this artisan command in our project.

What is Artisan command?

Artisan is the command-line interface available in Laravel. It provides a number of helpful commands that can help the developer you while building the application. It is driven by the powerful Symfony Console component.

Laravel command-line interface (CLI) is also known as Artisan. It was released with Laravel version 3 with limited number of functionalities, Once Laravel 4.0 was released and it used to composer-based architecture, new components were added in artisan command.

Artisan are mapped with different sub-commands of command line. We can also expand the fundamentals of artisan commands by generating new custom commands.

Uses of Artisan command:

Artisan commands can be used in many things, here are few examples: –

1. Managing database migrations and seeding
2. Publishing package asset.
3. Generate boilerplate code for new controllers and migrations
4. Create proper code skeletons
5. Insert user details in database.
6. Send feedback form to user after couple of months
7. Send a reminder to the user for tasks etc.
8. Send email and SMS notifications to the user.

Command ‘php artisan list‘ – When you fire this command in your terminal, you will get the list of commands which can be used in your Laravel project. In terminal, you will get Command with its description, with the help of description anyone can easily understand the use of command and its purpose. You will get, inbuilt and custom, both the commands in your terminal.

Writing custom artisan command:

In addition to the commands provided with Artisan, you may also create your own custom commands. Commands are stored in the app/Console/Commands folder.

Following command can be used to create custom commands.

php artisan make:command command name

Custom commands are used when we need to check anything or track the details or insert batch of data, send notification and feedback form to a user after couple of months.

Structure Of Command:

I. After generating the command, you need to fill out the signature & description of the class. Later, it will be used to display the command in terminal list.

II. The handle method will be called when your command is executed.

III. You may place your command logic in handle() method.

These three points explain the structure of commands, how you can write the commands and the fundamental structure of the commands. All the logical part of command available in handle method (method.handle), which is called when the command gets executed in the terminal. Basically you need to take care of handle() method, signature and description part. Once you have done with these three points, you need to register your command and it’s ready to use.

Steps to create custom commands :

1. Make sure Laravel is installed in your system.

2. Open up console and run the following command – php artisan make:command AppointmentNotificationOneDay

This command will generate file with the name of AppointmentNotificationOneDay.php file


namespace App\Console\Commands;

use Illuminate\Console\Command;
use Illuminate\Support\Facades\Mail;
use App\Mail\SendAppointmentMail;
use Carbon\Carbon;

class AppointmentNotificationOneDay extends Command
     * The name and signature of the console command.
     * @var string
    protected $signature = 'appointment:notificationoneday';

     * The console command description.
     * @var string
    protected $description = 'Send mail notification to user before 24hrs of appointment schedule.';

     * Create a new command instance.
     * @return void
    public function __construct()

     * Execute the console command.
     * @return mixed
    public function handle()
		// Logic will be written here

This file is generated in app/Console/Commands directory with the name of command, this class is always extending with Command class. Using this you can easily create as many files as you want.

3. Now its time to register our command, because it’s not registered yet. To register your command, navigate to app/console and open kernel.php file and add the below mention code in $commands array.


So kernel.php looks like:-

<?php namespace App\Console; use Illuminate\Console\Scheduling\Schedule; use Illuminate\Foundation\Console\Kernel as ConsoleKernel; class Kernel extends ConsoleKernel { /** * The Artisan commands provided by your application. * * @var array */ protected $commands = [ 'App\Console\Commands\AppointmentNotificationOneDay' ]; /** * Define the application's command schedule. * * @param \Illuminate\Console\Scheduling\Schedule $schedule * @return void */ protected function schedule(Schedule $schedule) { $schedule->command('appointment:notificationoneday')->hourly();

     * Register the Closure based commands for the application.
     * @return void
    protected function commands()
        require base_path('routes/console.php');

Congrats, now our command is registered on a console and we are ready to use this command in our application and website.

4. Run php artisan list command in your terminal, it will show the command name with the signature & a description which was previously defined in app/Console/Commands.

Conclusion :

With the help of following steps, we can create custom artisan command successfully. In this blog, we have introduced the artisan command and explained how you can use this excellent command line tool for setting up frameworks and classes.

If you would like to know more about the artisan command, you can drop your comment in comment section. One of our experts will get back to you for your help. cmsMinds, a leading PHP web development agency in NC, have built many applications using PHP using MVC framework. If you’ve any questions on this article, please let us know. Also, let us know if you would like us to write on any other topics on PHP and MVC framework that may be of interest to you.

Posted in PHP

Leave a Reply

Notify of