This website uses cookies

Our website, platform and/or any sub domains use cookies to understand how you use our services, and to improve both your experience and our marketing relevance.

CloudwaysCDN — a powerful solution that offers superior performance and satisfied global audience for your business. Read More

Create Custom Artisan Commands with Laravel

Updated on  19th January

4 Min Read
Reading Time: 4 minutes

Artisan is a command line utility that comes with Laravel, one of the most popular PHP frameworks.This utility is extensively used for setting up migrations, routes listings, queueing, class creations, and other related tasks.

Custom Artisan Commands with Laravel

For a complete list of tasks that Artisan could help with, run the following command inside the Laravel project directory:

Make is an important command in Artisan, used for creating custom Artisan commands.

If you have a task which often pops up during your projects, create an Artisan command, add the code into the class, and fire away from the CLI. Optionally, add the Artisan command as a cron job.

Lining up the Ducks in a Row

Let’s say you have a hypothetical service based app with a table named transactions. This table stores all the transactions of the app.

Every week, this script checks whether there are any unpaid bills for transactions in the last 30 days. If any such transactions are discovered, the system sends the user an email.

I will first create the boilerplate class using Artisan commands:

Next, register the class in app/Console/Kernel.php. In this case, It would look like the following:

The actual class is stored in app/Console/Commands/NotifyLateUsers.php. Here’s a quick overview of the class:

This is the actual command that will be executed from the command line for invoking the script.

The description which will show up with the command when  php artisan  is called.

The constructor to the command that is used for any initial setup.

This function contains the main body of our command.

At this point, run php artisan:

From here on, it’s just simple coding. I will first retrieve all records of users who have unpaid transactions. Then I will check if the due date has passed for that transaction. If it has, I will roll out an email. Here’s the code:

Notice that I added a function called mail_utf8. At the moment, I am sending mail through the PHP mail function. Because of this, the emails will most likely end up in the spam/junk folder. It would have been better to use a mailing service such as Mailgun, Mandrill, SparkPost, etc. Laravel supports some, if not most of the popular mailing services.

Everything else is pretty self explanatory. Here’s a sample run from the CLI:

And a finally, let’s check the inbox…



In this concise article, I introduced Artisan and then elaborated how you could use this excellent command line tool for setting up frameworks and classes. If you would like to contribute to the discussion or would like to know more about Artisan, please leave a comment below.

Share your opinion in the comment section. COMMENT NOW

Fahad Saleh

Fahad Saleh is a DevOps Engineer at Cloudways

Create Laravel apps without the worry of server management.

Deploy your app on optimized PHP hosting servers for Laravel.

Get Our Newsletter
Be the first to get the latest updates and tutorials.