Hi Artisan
In this tutorial i will explain about laravel task scheduling. I will show you step by step about laravel custom cron schedule. In this tutorial, i would like to show you how to setup task scheduling cron job in laravel 7 application.
In this tutorial we will first find out our inactive user and send them email notification to their email address using laravel custom command. We will send this notification using laravel task scheduling.
If you still don't know what is task scheduling and how to use it, then you are a right place. Task scheduling is a system where you can run a method after a couple of minute, second or hour.
we will create laravel custom cron schedule and you can run cron every seconds, every minutes, every hours or every days. you can also write database logic or send email notification using laravel cron job. i will give step by step example of laravel 7 task scheduler tutorial.
In this tutorial we will see how we can create our laravel custom command and using this command and using task scheduling with cron job, how we can send notification to our inactive user from our application.
Task Scheduling Example In Laravel
So for making it we have to add a new field in our user database table, So let's add a new field to our user database table.
Step 1: Setup Table
Now go to user table and paste this below code to your user table.
Now after adding it run migrate command to add this field. See here we add last_login field to check when was the user last logged in.
Step 2: Create Custom Command
For creating our custom command , run this below command
After running this command you will see this file to this following directory app\Console\Commands\EmailInactiveUsers.php.
Step 3: Setup EmailInactiveUsers.php
Open app\Console\Commands\EmailInactiveUsers.php and paste the following code.
Read more : Binding Data to Views Using View Composers in Laravel
app\Console\Commands\EmailInactiveUsers.php
Now see protected $signature = 'command:name'; this should be our custom command name and In handle() method , we have to setup our logic.
Step 4: Make Notification
Run this command to make new notification
After running this command you will see this file to this following directory app\Notifications\NotifyInactiveUser.php and paste this below code
app\Notifications\NotifyInactiveUser.php
Step 5: Setup EmailInactiveUsers.php
Open app\Console\Commands\EmailInactiveUsers.php and paste the following code.
Now if you run php artisan then you we see our custom command in laravel artisan command list.
Here we use this $limit = Carbon::now()->subDay(7); statement to get who logged in 7 days before and then send them notification.
Step 6: Setup Auth Controller
To save last_login field data, go to your login mechanism and paste this below code to your login attempt method.
See here we've saved current time when a user is going to log in.
Setup Task Scheduling
Step 7: Register Our Custom Command class and Scheduler
To register our custom command , go to app\Console\Kernel.php. In Kernel.php , register our command class inside $commands array.
app\Console\Kernel.php
and paste this code to control schedule.
Look in schedule method, we can define how many time later we want to process our schedule. To know more you can visit below links.
So our ultimate Kernel.php will look like this
Now if you run
Then our logic will work. That mean our notification will be sent to those user who logged in 7 days ago. hope you got it.
Step 8: Run Scheduler Command For Test
now we are ready to run our cron, so you can manually check using following command of your cron. so let's run bellow command:
At last you can manage this command on scheduling task, you have to add a single entry to your server’s crontab file:
I hope it can help you.