How To Create Custom Artisan Command in Laravel 5

This laravel tutorial help to create new artisan command into laravel 5.Artisan is awesome feature of laravel. Artisan is the command-line interface of laravel.Laravel provides a number of helpful artisan commands for your use while developing your application.

Internally, We will create command-line functions that you will interact with your Laravel application using CLI.

There are following steps will follow to create artisan command -

  • Create Artisan Command Class file into app/Console/Commands folder
  • Make Entry into kernel.php file

Artisan is based on Symfony Console component.This laravel 5 tutorial help to create your own artisan custom command and attached with artisan laravel command list.
$php artisan make:console helloCmd --command=hello:name

Note: Its renamed to make:command for laravel 5.3+.

create-custom-php-artisan-command

You can get all available artisan command for your apps using below command:

php artisan list

You can also get help as well using below command, the below command will display and describes the command’s available arguments and options.
php artisan help migrate

You can get laravel version using command line:
php artisan --version

You can get laravel current environment using command line:
php artisan env

We will build own custom commands for working with our application. You can store your custom commands in the app/Console/Commands directory or your own storage location as long as your commands, for custom storage location, You need to autoloaded custom artisan command based on your composer.json settings.

You can also check other recommended tutorials of Lumen/Laravel,

Custom Artisan Command in Laravel 5.2

To create a new artisan command, We will use inbuilt php artisan command make:console, which will generate a command stub into app/Console/Commands directory:

php artisan make:console simpleArtisanCmd --command hello:name

The command above would generate a class at app/Console/Commands/simpleArtisanCmd.php.I have used --command option to assign the terminal command name.We will open this file and modify signature and description properties for this artisan command, Which will display at the time of listdown all command.

argument('name');
          $this->info("Hello! Mr. $name, I Hope Your are Doing Good.");
    }
}

The handle() method will be called when your command is executed.You need to add your logic here that will execute when the artisan command will fire from CLI.

We will register above console command class into Kernel.php. You need to add one entry into $command section in app\Console\Kernel.php file.

protected $commands = [
        // Commands\Inspire::class,
		Commands\simpleArtisanCmd::class,
    ];

Now, You will run php artisan list from project command line, please make sure you are in laravel project then run list command.You will see below description for hello command under in command list:

hello
  hello:name          This will display simple hello message with your name.

The description has been given into above php class file.

How To Run Laravel Artisan Command

Now, We will test our php artisan custom command.We will run command using below option:
php artisan hello:name parvez

You will get below message into command line:
Hello! Mr. parvez, I hope your are doing Good. Bye!

Leave a Reply

Your email address will not be published. Required fields are marked *