Learn about Task Schedule in Laravel

Tram Ho


Hello everyone, I am back again!

You probably know (if you don’t know now) Bill Gates’ famous quote:

“I choose a lazy person to do a hard job. Because a lazy person will find an easy way to do it. ”

Because it’s lazy it’s human nature already, because of being lazy, I’ve neglected this Laravel series for a long time. And thanks to that laziness, I decided to choose to write about Task Scheduling to guide newbie brothers to solve the “laziness” when doing projects.

Just like that, let’s start!


What is Task Schedule

Task Schedule name sure enough to say what is its function, right “Scheduling jobs”. That is, you will be ready to do the work to be repeated in a certain cycle and assigned to the server to perform instead of having to run on “rice”.

If you have registered a genuine mobile phone sim, then on your birthday you will surely receive a message of affection like this.

And the problem is, every day hundreds of thousands of customers have a birthday, and this will have to be repeated daily until “no one uses that network anymore”.

And the solution to that problem is the Task Schedule !

How to use the Task Schedule


First to use the Task Schedule, you must understand the concept Cron (Cronjob) has (it’s like to know ahead run, they must know, want to know right away they know this cow).

Cronjob is a tool that automatically implements jobs / tasks automatically in a certain cycle on the system, specifically here on the server (Unix / Linux).

And the jobs / tasks to run automatically are declared in a file called crontab, and to work with this crontab you will need the following commands:

After creating / opening crontab file, we need to understand the structure / syntax of crontab. It should look like this:

If you ask me why there is a starting from 0 and some start from 1, I do not know, simply because the person who created it ???

And if you ask why it is not only a numeric value but it also receives the value * , I can explain, when using * it will receive all possible values, for example * in the first position. ie command / job that will run once a minute.

Task Schedule in Laravel project

Okay, that’s enough to say. Now we will come to the main part of the article, which is to use the Task Schedule in the Laravel project.

To run a command, you must first create a command first, and the divine command php artisan again appears. ?

After creating, go to the path app/Console/Commands/TenCommand and add the code you want to run into it, it will look like the following

Once you have the command, you will need to declare it in the Kernel.php file, specifically in the function schedule() , where you will declare the frequency of running the command as follows:

In addition, to declare the frequency of running commands, there are some common options such as

  • daily() : run the command daily at 12:00
  • hourly() : run the command for hours
  • cron('* * * * *') : run the command on a custom schedule, like the above declaration

To learn more about other declared frequencies (suitable to your needs), you can refer here.

The command declaration in the project has finished, now let the server be declared:

  • Run the command

  • Add to the line

If you understand what I wrote above, I will understand that this line informs the server that it should perform schedule:run every 1 minute.

And so, all the commands declared in function schedule() will also be executed in accordance with the frequency that you declare.

Too simple is not it!


In short, I firmly believe that the task schedule will contribute a lot in your Website, whether it’s the actual product or just a project to study.

And if you find the article useful, please upload or clip to easily review it, and if the article is still not satisfied you, leave a comment to answer and improve more in the following posts!

See you in the next posts in this Laravel series , I promise to update more posts more often!

Thank you very much!


Share the news now

Source : Viblo