In this tutorial, we will explore task scheduling in Laravel using Artisan. By leveraging the power of Laravel's built-in command scheduler, you will be able to automate various tasks such as sending emails and cleaning up the database.
What will you learn?
- How to define your command schedule within Laravel
- Automate tasks such as emailing and database cleanup
Prerequisites
- Basic understanding of Laravel and PHP
- Laravel environment setup on your machine
In Laravel, the command scheduler is built on top of the Artisan command-line tool. It provides a fluent, expressive interface for defining command schedule within Laravel itself.
First, create a new command using the make:command
Artisan command. For instance, if you want to create a command that clears the old data in the database:
php artisan make:command CleanDatabaseCommand
This command will create a new command class in the app/Console/Commands
directory.
Next, within the command class, you need to define the command's behavior in the handle
method:
public function handle()
{
DB::table('old_data')->delete();
}
The handle
method is called when the command is executed. You can put your task logic in this method.
Finally, schedule your command in the schedule
method of the App\Console\Kernel
class:
protected function schedule(Schedule $schedule)
{
$schedule->command('database:clean')->daily();
}
Let's put all these pieces together to create a task that sends a daily email:
// app/Console/Commands/EmailUsersCommand.php
<?php
namespace App\Console\Commands;
use Illuminate\Console\Command;
use App\Mail\DailyReport;
use Illuminate\Support\Facades\Mail;
class EmailUsersCommand extends Command
{
protected $signature = 'email:users';
protected $description = 'Send a daily email to all users';
public function handle()
{
$users = User::all();
foreach ($users as $user) {
Mail::to($user->email)->send(new DailyReport);
}
$this->info('Daily emails have been sent');
}
}
Then, schedule this command in the Kernel class:
// app/Console/Kernel.php
protected function schedule(Schedule $schedule)
{
$schedule->command('email:users')->daily();
}
This will send a daily email to all users.
In this tutorial, we’ve learned how to use Laravel's command scheduler to automate tasks. We've seen how to define a command's behavior and schedule it to run at specified intervals.
For further learning, you can explore other scheduling methods provided by Laravel, such as hourly
, twiceDaily
, and weekdays
.
Exercise 1:
Create an Artisan command that clears the cache once a day.
Hint: Use the Cache::flush();
method to clear the cache.
Exercise 2:
Create an Artisan command that backups the database every week.
Hint: Use the Artisan::call('backup:run');
method to back up the database.
Exercise 3:
Create an Artisan command that sends an email to inactive users once a month.
Hint: You can define inactive users as users who haven’t logged in for the past month.
Remember, practice is key to mastering Laravel's task scheduling. Keep exploring and happy coding!