Laravel: Task scheduling, Notify/Remind user for upcoming event

Manash Chakrobortty
2 min readDec 29, 2020

--

We are very familiar with Google event reminder, we get email or other kinds of notification before the actual event. It is really helpful, since we are very busy with our day to day activity. But how can we implement this for our self using awesome features of Laravel. Here are the basic steps we can follow to create a simple notification scheduler. Though we can configure the scheduler in more sophisticated way, but we will just focus on basic.

Create command

To create a command you can run:

php artisan make:command MeetingReminder

set the signature of the command like:

protected $signature = 'users:meeting-reminder';

So now we can run this command in our terminal like:

php artisan users:meeting-reminder

Inside the handle() method write all the business logic for the notification, also we can use existing notification or create a custom notification. Here is the sample MeetingReminder.phpcommand file.

Register command & Task scheduling

In the Console/Kernel.php class we can schedule the command. First we need to add it to the $commands array:

protected $commands = [
MeetingReminder::class,
];

And in the schedule() function we can schedule the command to run in every minutes.

protected function schedule(Schedule $schedule) {
// Here you can execute the command once every day
$schedule->command('users:meeting-reminder')->everyMinute();
}

Run scheduler locally

In the development phase we may need to run & test the scheduler locally. For this we will not require to add scheduler cron entry, instead we can use schedule:work command.

php artisan schedule:work

This command will run in the foreground and invoke the scheduler every minute until you terminate the command.

Run scheduler on dev/production server

We only need to add a single cron configuration entry to our server that runs the schedule:run command every minute. On Ubuntu use the command

crontab -e

to open your cron job file, then add

* * * * * cd /path-to-your-project && php artisan schedule:run >> /dev/null 2>&1

All done :)

--

--

Manash Chakrobortty
Manash Chakrobortty

No responses yet