Laravel tutorial - Laravel Scheduler

Learn how to automate tasks in your Laravel application using the built-in Task Scheduler. This tutorial guides you through creating a custom Artisan command to perform a recurring task, such as cleaning a database table. Discover how to define the command's logic, register it…
Key Concepts of the Laravel Scheduler\n\nThis document summarizes how to create and run automated tasks using Laravel's built-in scheduler.\n\n### What is the Laravel Scheduler?\n\nThe Laravel Scheduler is a feature that allows you to programmatically define and manage recurring tasks for your application. Instead of creating multiple cron entries on your server, you define all scheduled jobs within your Laravel code, requiring only a single cron job on the server to trigger the scheduler itself.\n\nCommon use cases include:\n- Sending daily email reports.\n- Cleaning up database tables or logs weekly.\n- Performing any recurring job at a specific time or interval.\n\n### The Core Workflow\n\n1. Create a Command: Build a custom Artisan command that contains the logic for the task you want to automate.\n2. Write the Logic: Implement the task inside the command's
What is the Laravel Scheduler?
Hi everyone, welcome back to the Laravel video tutorial. In this video tutorial, we are going to learn about the Laravel Scheduler. \n\nSo first question is, what is the Laravel Scheduler? As the name pronounced, if you want to schedule something for some other time, then you can schedule it here. Let's say you want to schedule that the database logs will be cleaned after every week, or you want to say that you want to send a mail to every user on a daily morning at 7:00 p.m. or anytime that you want. \n\nSo for that, it is not possible that a programmer will be sitting there and sending a mail to everybody, or a program already made it there, and we have to hit it every day in the morning. Right? So for that, we will schedule a job, and this job will be done when the time will come. Let's say for every weekend, for every day, even every minute, or even yearly, whenever you want to schedule that, you can do that.
Understanding the Workflow
Alright, so this is the basics about the scheduler, and for now we are going to make a scheduler and how it's going to work. Alright, so for that scheduler, we actually need a cron job that will be hit after every time. Alright. But we don't have a server for now, so I'm just going to show it with another example, not with the actual cron. Alright, so there is a command in Laravel which is the schedule:run. So we will run the schedule with this command and it will work. Alright, so now let's get started with that. \n\nSo when we are just going to start with a scheduler, we have to need to do some steps. First of all, we need a command, actually. Alright, so we will make our own custom command. Then we have to, we will give the custom command a name and description also. And then in a function side, we will write some code. That code will be executed when we just run this command. Alright, and this command will be run by the scheduler. Alright, so you will see that everything will be going on very dynamic.
Defining the Task and Creating a Command
And what task actually we have to do? In this video, we will run a scheduler, and in that we will just clean our DB table. Alright, so now let's start. So first of all, I'm just going to show you my database, which is user, and there is a table with the name of tests. Alright? There is no data here. So let's say I just make some entries here, like... it's an ID, so let's say 1, 2, and 2, Sam. Alright. So when I just write a program, when I run this scheduler, this table will be cleaned automatically, right? And it will tell you on the command line also. Okay, so now just start with that. So first of all, I'm just going to make a command. First of all, let's clear everything and just write here php artisan make:command and the command name. So let's say my command name is EveryMinute because I want to schedule the task in every minute, because if I just go for the hour, it's not possible in a video. So just write here EveryMinute and hit the enter.
Configuring the Artisan Command
Then you can see that your command is successfully created. Where is this command placed? Inside the app/console/commands and EveryMinute. Alright, so now we have to write the command name. So for that, we have to just put the name on the signature. So let's say my command name is minute, and let's put update or delete, whatever you want to put. I just put the update here. And here we can put a description. Let's say, 'This will clean a DB table.' Alright. Okay, so our command is created now.
Implementing the Command's Logic
So now, in the handle method, we have to write the code that we want to execute. Okay? So let's say I want to clean my DB, so for that I have to put that name and write the command for that. So just write DB, and with the DB table, and the table name is tests, as we've seen, and just call delete(). The command will not delete the table; it will delete the entries of the table, right? Okay, and after that, I can write here something that's like, 'Operation done.' Fine. So I just write DB, so I have to import the DB facade also. So just you can use Illuminate\\Support\\Facades\\DB. Just find support... alright, and now... okay. Alright, so everything is done here.
Registering the Command and Manual Test
So now what we have to do, the last thing: just go inside the Kernel, and here we have to execute this command. Okay? So for that, what we have to do? Just clean it, and here we have to write the command name. What is our command name? As you see, my command name is minute:update, and hourly I can just change here to everyMinute(). Alright, so I think everything is fine. \n\nSo now let's go to the terminal and just write here php artisan list and let's see if our command is existing here or not. Alright, so just go to the minute. Yes, so this is the command is here: minute:update. Okay, and you can see that, 'This will clean the DB table.' Right? Just copy it here and I'm going to write here php artisan... Okay, and before that, we have to do one more thing, actually. I have to write the DB configuration also, which is... I forgot to write here. Okay, so my database name is user, username is root, and password is password@123. It is a very common name. Okay. And then, I just have to restart the server command, because otherwise it's not going to pick this configuration from the environment file. And just hit the enter. And you can see that we got the 'Operation done' here. Alright, so the operation should be done. So there were two entries, they should be cleaned. Let's check. Yes, you can see that this is cleaned.
Running the Scheduler
Alright, so you can see that we just have run this command, but how actually does the scheduler work? This command we are not going to have to run directly, manually. This will be run automatically by the scheduler. Okay? So just for that, I am just going to make some another entries, then I can tell you how it actually works. So let's say 'three', 'Bruce'. Alright, so I think one entry is enough for now. Alright. So now I'm not going to run this command. I am just going to run the scheduler. Okay? So we just write here php artisan, and here I have to write schedule:run. And just hit it. Then you can see that it will tell that, 'Running scheduled command: php artisan minute:update'. It will run this command. Why? Because here we write inside the kernel this command here, okay? And it's automatically run when we just put it on the server, and then it will automatically run this command in every minute. Why every minute? Because we put it here, everyMinute(). Alright. So and it will clean the table every minute. So you can see that it's clean from here.
Exploring Documentation and Conclusion
So now just take a look from the Laravel Scheduler documentation so that I can just tell you some more exposure here. Okay, so in the introduction, this is telling you what it is, and it will tell you how to start the scheduler. For the scheduler, we have to write this on the server. If you want to go there, then you can learn from here also. And then we have the same thing that we have to supply there, like this. And then you schedule the Artisan commands like here. Alright, and there are some functions like everyMinute(), everyFiveMinutes(), everyTenMinutes(), fifteen(), and all the things are here. Alright, so you can choose whatever you want. You can choose yearly, daily, monthly; it will run whenever you choose. Alright. So they will be automatically run after the whole time when they are scheduled in the cron job from the server. \n\nOkay, so that's it for now. So thanks for watching this video. If you have any kind of confusion, you can ask me in the comment box, and don't forget to subscribe our channel. Stay tuned with us. Thanks, guys. Thanks for watching this video.