In any web application, we need specific administrative tasks that run periodically and doing that manually is not a good idea. Whether you want to send out emails to your customers on particular events or clean up the database tables at the specific time, you will need a task scheduling mechanism to take care of the tasks. Cron is a task scheduler in UNIX-like systems, which runs shell commands at specified intervals.
If we want to schedule tasks that will be executed every so often, we need to edit the Crontab. Crontab is a file that contains a list of scripts that will run periodically. Cron is a task scheduler daemon which runs scheduled tasks at specific intervals. Cron uses the configuration file called crontab, also known as cron table, to manage the scheduling process. Crontab contains cron jobs, each related to a specific task. Cron jobs are composed of two parts.
1. Cron expression
2. Shell command to be run
So what we do here is create a command using Laravel Console and then schedule that command at a particular time. When using the scheduler, you only need to add the following Cron entry to your server.
First, we download the freshlaravel and then start working on the example.
Step 1: Configuring the Laravel
Type the following to generate boilerplate of Laravel 5.6.
Okay, now edit the .env file to configure the database.
Now, migrate the tables into the database.
Step 2: Create User Authentication
Laravel provides Authentication system out of the box. Just hit the following command.
It will generate authentication scaffolding.
Next step, start the Laravel development server using the following command.
Go to this URL href=”http://localhost:8000/register Register one user.
Step 3: Create Laravel Artisan Command
We use the make:console Artisan command to generate a command class skeleton to work with. In this application, we will just send one email to the owner telling that, we have these number of users registered today. So type the following command to generate our console command.
The above command will create a class named RegisteredUsers in a file of the same name in the app/Console/Commands folder. We have also picked a name for the command via the command option. It is the name that we will use when calling the command. Now, open that command file RegisteredUsers.php.
We have just changed the description of the command. Now, we need to register this command inside app >> Console >> Kernel.php file.
Go to the terminal and hit the following command.
You can see in the list that your newly created command has been registered successfully. We just now to call in via CronJob and get the job done. Now, write the handle method to get the number of users registered today.
Next step, we need to send an email that contains that totalUsers. So let’s create a mail class.
Step 4: Create a Mailable Class to Send the Mail
Type following command to generate mail class.
So, It will create this file inside App\Mail\SendMailable.php. Now, this class contains one property, and that is count. This count is the number of users that registered today. So SendMailable.php file looks like this.
Also, define the view for this mail at resources >> views >> emails >> registeredcount.blade.php file. The mails folder is not there, so need to create one and then add the view registeredcount.blade.php.
Now, add this mailable class inside RegisteredUsers.php file.
For sending a mail, we used Mailtrap. You can quickly signup there. It is free for some usage. It fakes the email, so it is convenient to test our application.
Now, type the following command to execute our code. Let us see that if we can get the mail.
we are getting the email that is saying that a Total number of registered users for today is: 1. Now schedule this command via CronJob.
Step 5: Scheduling the Commands
Let’s schedule a task for running the command we just built.
Our task, according to its usage, is supposed to be run once a day. So we can use the daily() method. So we need to write following code in the app >> Console >> Kerne.php file. We will write the code inside schedule function.
If you want to more info about task scheduling, then please refer the documentation.
When using the scheduler, you only need to add the following Cron entry to your server.
For us in localhost, we will hit the following command to get the mail of users.
After a minute, we can see that we are getting one mail that contains some users registered today. You can see more time interval here. So this is how you can configure the cron job in Laravel. Finally, our Laravel Cronjob Scheduling Tutorial is over.
Wants to learn more about Laravel? We are providing complete solutions related to Laravel for your Websites. Contact us to know more about the Cron job in Laravel.