Creating your own Artisan Commands

We, as a developer, would like to automate many things in our project, like sending emails, newsletter, backing up database remotely (you know how much important that is, right?).

In Laravel, we can do these automation using commands and by calling those commands in your cron.

Let’s discuss this.

Creating New Command

It is fairly easy to a create a new command in laravel that is custom for you to manipulate and call whenever you want to in the application life cycle.

Why is it easy to create, you say? Laravel has given a command to create a command, isn’t that great?

php artisan make:command CommandName

Using the above command, you will have a new file in your app/Console/Commands directory.

The file generated should look like this:

Image from Carbon.now.sh

We just need to change the $signature and $description variables to our need.

A classic example is to send emails like newsletter, or particularly to one person.

$signature = 'send:Email';
The handle function would be executed when this command is fired, so you can put your logic in the handle method of this class.

But this send:Email is not enough, what if, we want to send an email to a particular person, how can we specify that?

That’s a great question, this is where arguments and options come in handy.

Arguments and Options

The only difference between arguments and options are that arguments are mandatory and options are from name, optional.

You can define arguments like:

$signature = "send:Email {user}";

{user} is the argument that we need to specify while executing the command, or it will throw an error that it is required.

Now, you can define options like:

$signature = "send:Email {user} {--newsletter}";

Every option starts with “ — ” (double dashes). The options can be of 2 different types, one which expects a value and one which one doesn’t.

The above { — newsletter} doesn’t expects a value, if it is their in execution statement, then it’s value will be true, otherwise false.

You can define an option which expects a value like:

$signature = "send:Email {user} {--newsletter=}";

Just that ‘=’ makes the difference, now, when your newsletter option is their in the execution statement, it will require some value otherwise will throw an error.

The execution statement would look something like below:

php artisan send:Email 3 --newsletter=daily

Arrays as Inputs

If we want arrays as inputs to our argument or options, we can do something like:

$signature = "send:Email {user*}";

What this will do is accept strings separated by space as an array for the user argument.

php artisan send:Email one two three

This will give $user = [‘one’, ‘two’, ‘three’].

Now when we define an option with an array input, we need to specify the option name every time we have a new value for it. Like below.

$signature = "send:Email {--newsletter=*}";
//Calling the above
php artisan send:Email --newsletter=daily --newsletter=weekly

Now, the only thing left is to register this command, so it will come up in

php artisan list

which displays all the commands that we can use.

Registering the command

You can do this by adding the class namespace to $commands array in you app/Console/Kernel.php.

protected $commands = [
Commands\SendEmail::class
];

Now, your command is registered and ready for use.

We will discuss how to get the values of arguments and options and how to use them in the next article.

Hope you like this one. Have a codeful day.

Don’t forget to clap for this article, and share it with your friends and colleagues and always love open-source.

Like what you read? Give Vipul kumar basapati a round of applause.

From a quick cheer to a standing ovation, clap to show how much you enjoyed this story.