Laravel 5.5 create a first artisan console command

Laravel is the most popular PHP Object Oriented framework. Laravel provides awesomely artisan console functionality. Laravel artisan console provides lists of commands that can assist you while building your application. So, How can we create a first artisan console command?

Let’s see today we learn how to create artisan console command. Laravel artisan command allows us to create the new artisan command using “make:command”.

So, Let’s create the new artisan console command class using below command.

php artisan make:command TheCodingStuff

This command generate new class file “TheCodingStuff” at “app/console/commands” directory. Now, let’s open the “TheCodingStuff.php” file and change it like.

<?php

namespace App\Console\Commands;

use Illuminate\Console\Command;

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

    /**
     * The console command description.
     *
     * @var string
     */
    protected $description = 'The Coding Stuff Hello Word! example';

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

    /**
     * Execute the console command.
     *
     * @return mixed
     */
    public function handle()
    {
        $this->line("Welcome to TheCodingStuff!");
    }
}

Now let’s, change the generated class file and create our custom artisan command.

First of all, change the command name add your new command name as you want to “$signature”. Right now, I set the “thecodingstuff:hello” for my command name.

Then add the description for your command to “$description”. The description is most important for the user to get the information regarding command.

the “handle()” function is for giving and take feedback from the user. Right now I just write the Welcome line on this function using “line” method. The “line” method helps to print the plain text for the user.

Now let’s, register our command for artisan console. Let’s open “app/console/kernel.php” file and register our command like below.

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

Our command is ready for used. Now let’s check our command is listed on artisan console command list.

php artisan list

This command print lists of the artisan command, Now let’s fire custom command like

php artisan thecodingstuff:hello

This command print the output like Welcome to TheCodingStuff!

If you face any issue with creating your custom artisan command write the comment below.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s