Out of the box, Laravel comes with several commands that are available to use while developing an application. For example, php artisan serve, php artisan make:auth, php artisan migrate, etc. But as our application grows, we might find some tasks that are repetitive or tasks that can be done in background by scheduling them or some tasks that, we as a developer, are doing everyday and can be automated by creating a custom command.


So, let's learn how to build Custom Artisan Command.


Assuming you have already installed Laravel, open the terminal, go to the root folder of your application and let's execute the below command:

php artisan make:command appSetup

Above command would create a new file named appSetup.php inside app/Console/Commands directory with following content:

<?php
namespace App\Console\Commands;

use Illuminate\Console\Command;

class appSetup extends Command
{
    /**
     * The name and signature of the console command.
     *
     * @var string
     */
    protected $signature = 'command:name';

    /**
     * The console command description.
     *
     * @var string
     */
    protected $description = 'Command description';

    /**
     * Create a new command instance.
     *
     * @return void
     */
    public function __construct()
    {
        parent::__construct();
    }

    /**
     * Execute the console command.
     *
     * @return mixed
     */
    public function handle()
    {
        //
    }
}

Let's set our command name to the $signature variable and respective description to $description variable.

protected $signature = 'app-setup';
protected $description = 'Setting up an application';

Great, now let's check if our command is registered and appearing appearing in the list of artisan commands or not by executing the below command:

php artisan list

Hmm, above command shows all existing artisan commands but not the one that we just created:

http://milanchheda.com/uploads/list-of-artisan-commands.png

Ok, to register out command, open the Kernel.php file available inside app/Console directory. We need to add our Commands\appSetup::class to the $commands array, so it should like this:

protected $commands = [
    Commands\appSetup::class,
];

Now, if we run php artisan list, we should be able to see our command signature.


Great, now let's code for what should happen, when our command is executed. In our appSetup.php, you will notice there is a handle() method. This method takes care of the functionality that should be performed when our artisan command is executed. So let's add below code inside the handle() method:

public function handle()
    {
        $this->call('config:cache'); // this will cache all your configurations
        $this->call('migrate:refresh'); // this will rollback and exectue the migrate command
    }

Now just execute the custom artisan command that we created and see the magic:

php artisan app-setup


Fantastic, in less than 15 minutes, we learned how to create a custom artisan command that when executed will cache all the configurations and will re-migrate the database for us. Excellent, right? This is just one-of-the many cool things that can be done using artisan commands and can greatly reduce your time in performing repetitive tasks or executing a sequence of commands.


Happy Coding! :)