Laravel Custom Commands


Uses :

Custom Commands is used when we need to check something or track something on particular time period, for example : we need to send mail to notification mail to user when his subscription period is near to expire or we need to send feedback form to new user after one month to know their experience, etc.

So in this case we will create custom command and we set cron job that execute our command.

Now the question is how to make custom command?

Laravel Initial SetUp

Let’s start with a new fresh project.
*For Understanding Purpose we make it simple by using only two tables “users and posts”

  • Create new project
  • Set up SQL connection in .env file
  • Make new migration table : posts
  • Add few columns like title, description, user_id(foreign constraint)
  • Migrate database
  • Make Eloquent Model For User(if not exists) and Posts

Set Up Relationship (*optional)

1) Define relationships and add accessor in Post Eloquent Mode. (*optional)

Same as Post Eloquent, add relationship and accessor in User Eloquent Model

2) Add few records in a database. (manually from phpMyAdmin)
3) As of now, the Basic app is to understand the custom commands.

Simple Commands

Generate Command File

Structure Of Command

  • After generating your command, you should fill in the signature and description properties of the class, which will be used when displaying your command on the list screen.
  • The handle method will be called when your command is executed.
  • You may place your command logic in this method.

Here one new command file is created : App\Console\Commands\allUsers.php

Now Modify our new command file:

Signature Means : The command used after php artisan

So change signature variable to :

Change the description :

This message is displayed in the help section of our command.

Now, an actual part of the command is here:

We need to add our logic part at

In this example we need to select user details, so add :


  • If You Want to display details in tabular format, “toArray()” is required.
  • In the table() we need to pass the column name(s) and array for that columns.
  • Artisan CLI can manage the output as a table for GUI purpose (if needed).

Output :
Now, our command is ready to execute.
Go to terminal and execute the command


Print Details On Command Line :

Get Input In Through Command Line

As of now we know how to fire custom command, now understand how to get input from command line and process that input.

Let’s make new command which fetches particular user’s details.

Change signature of our command :

Note :

  • Here, {variable : description} is used to get input from user.
  • As per our signature command will look like
  • Here we can also pass optional values by adding {id?} so “?” is used for optional values.
  • If you want to add some options then use {–option_name}, if you want to add shortcut then {– A | argument } and you can access that value by using switch case
  • If you have not fixed a length of output then you can use an array for it by adding *.
    Example :

    • COMMAND FILE : $signature = ‘user:get {– id =*}’;
    • TERMINAL : php artisan user:get –id=1 –id=2

Now add our logic part / task at

To use input of CLI :

In this example we need to select user details, post details, so add :

In above example we add ProgressBar, ProgressBar is used to get tracking information and nothing else.

  • First, we need to init ProgressBar with max.
  • After that just use advance() with an argument, the number of increment default is 1.
  • To complete the use of ProgressBar, use finish() to end that ProgressBar.
  • ProgressBar Is Completely Optional. It is only for GUI Purpose Only!

Output :
Now, our command is ready to execute.
Go to terminal and execute the command



Conclusion : –

That’s It! I hope you found out my article useful. For any suggestions, please hit comment below.

Thank You!


Krunalsinh Jadeja Web Developer

I'm Web Developer At Yudiz Solutions Pvt. Ltd. - a leading Web, Mobile Apps and Game Development Company. I have knowledge of Laravel, CodeIgniter and, Core PHP. I like to work on challenging tasks and always ready to learn new things. I like to get in touch with people and do something innovative with them.

Comments are closed.