Creating your own Artisan Commands

Vipul Basapati
3 min readMar 12, 2018

--

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.

Laravel And JS Resources

Below are some resources for Laravel and Javascript and my favorite JS framework: React.

Laravel

  1. Official Docs are the best maintained for Laravel. And Laracasts is the best.

Javascript

  1. For learning Javascript, Wes Bos is the best one to teach, because I have taken at least 6 courses from him. And have everything positive. You can learn the basics of JS and get your hands-on Vanilla Javascript using JavaScript30 course.
  2. You can learn the new syntax or the modern Javascript syntax called ES6 from es6.io. (Paid)
  3. You can learn React from scratch by learning from this tutorial on ReactForBeginners.com. (Paid)

--

--