Laravel Create Your Own Custom Artisan Command

Laravel provides a variety of artisan commands to automate tasks like making controllers,  migration, and model. But when you work on an application, Sometimes you need to create your own custom command for some automated task.You are not limited to Laravel's commands; you may write your own in a few simple steps.


Listed below are the methods for creating a new artisan command.


Step1: First create a new laravel application with below create command.


composer create-project laravel/laravel demo-app


Step2: Create Command.

To make a new command, use the make:command command. Just type the name of the command, like this:


php artisan make:command CheckUsers


In this example, we're making a command called CheckUsers. The command makes a file called CheckUsers.php, which is named after the command, and puts it in the app/Console/Commands folder


Step3: Modify the command.

You must first configure the command signature. This is the recommended command line argument to use after php artisan. For this example, we'll be using check:users, which can be accessed with the command:


php artisan check:users


In order to do this, update the following change to the command's $signature property:


protected $signature = 'check:users';


Next, you will need to provide an appropriate description for the command that will be shown among other commands when using php artisan list.


To do this, update the following changes to the $description property:


protected $description = 'Get the count of users';


Then, in the handle() function, carry out any action you desire. The number of users is echoing in this scenario.


public function handle()
{
    echo User::count();
}


Step4: Check the command.

To find out how many people are currently into your system, type the command in the console.


php artisan check:users

Supplying Arguments to a Command

You might have a command that requires a function argument. For example, a user's id would be needed for a command to delete all of that user's posts from the database. To pass an argument, update the $signature variable and write the argument between curly braces.


protected $signature = 'remove:posts {userId}';


Then you can run the above command like below:


php artisan remove:posts 5


Sometimes, you want to be able to skip an argument, but not always. You can make the argument optional by adding a question mark towards the end, as shown in:


protected $signature = 'remove:posts {userId?}';


You could also define a default value for the argument like:


protected $signature = 'remove:posts {userId=6}';


You can also set more than one argument, and you can make them optional or give them default values.


protected $signature = 'remove:posts{userId} {$userAge?} {$userStatus=alive}'


You can access those arguments by:


$this->arguments()


An associative array containing parameters as keys and their values as values is returned by this. In order to retrieve the userId parameter, do as follows:


$user_id = $this->arguments()['userId'];


There is another approach to just get one argument, though:


$user_id = $this->argument('userId');

Supply Option To Command

Options could be given to the command. Similar to arguments, options are used to enrich commands with additional information. It's can used without arguments


For instance, you might provide the command the –verified argument to only count people whose emails have been validated. If you want to construct an option, provide the argument's signature into the $signature property instead, but add the –prefix.


protected $signature = 'check:users {--verified}';


Currently, the command may be used with the following option:


php artisan check:users --verified


You may provide a required value or a default value for an option.


protected $signature = 'check:users {--verified} {--add=} {--delete=5}';


In this sample, delete has a default value of 5, but add needs the usage of a value. Whether or not verified is passed in determines what value is given to it as a Boolean.


If you want to retrieve the value of a single option, use $this->option('verified'), and if you want to get all the choices as an associative array, use $this->options().

Add Description to Parameters

Up to this point, we have discovered how to take arguments and even options; but, when the command is executed with php artisan help, these inputs do not provide any explanation. To configure them, you add a colon, ":," after the name of the argument or option.


protected $signature = '
        check:users
        {userId: Id of user to be fetched}
        {--verified: Gets count of verified users}
    ';


Now, if you run php artisan with the –help check:users you would see something similar to this: