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.
Step2: Create Command.
To make a new command, use the make:command command. Just type the name of the command, like this:
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:
In order to do this, update the following change to the command's $signature property:
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:
Then, in the handle() function, carry out any action you desire. The number of users is echoing in this scenario.
Step4: Check the command.
To find out how many people are currently into your system, type the command in the console.
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.
Then you can run the above command like below:
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:
You could also define a default value for the argument like:
You can also set more than one argument, and you can make them optional or give them default values.
You can access those arguments by:
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:
There is another approach to just get one argument, though:
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.
Currently, the command may be used with the following option:
You may provide a required value or a default value for an option.
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().
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.
Now, if you run php artisan with the –help check:users you would see something similar to this: