January 16, 2024 · 3 min read · 322 views
We're excited to announce that we have updated all of our blog post examples to reflect the new Laravel 11 version! Our previous examples were based on Laravel 10, but with the release of Laravel 11, we wanted to ensure that our readers have access to the most up-to-date information and examples.
When managing an app, there are routine tasks that need to be run periodically, such as cleaning up old posts, optimizing images, or sending out email digests. Performing these tasks manually can be time-consuming and error-prone. Laravel's task scheduling provides an elegant solution for automating such tasks.
Laravel's task scheduler allows you to fluently and expressively define your command schedule within the application itself. Instead of creating multiple Cron entries on your server for each task, you only need to add a single Cron entry that executes Laravel's scheduler.
This scheduler is located in the app/Console/Kernel.php
file, where you can define all your scheduled tasks. Laravel will then evaluate these tasks and run them when they are due.
The first step is to add the following Cron entry to your server. This entry calls the Laravel command scheduler every minute:
* * * * * cd /path-to-your-project && php artisan schedule:run >> /dev/null 2>&1
Replace /path-to-your-project
with the actual path to your Laravel application. This single Cron job is the engine that drives the execution of all your scheduled tasks.
With the Cron entry in place, you can now define your scheduled tasks. Open the routes/console.php
file and write the schedule
method. Here's where you'll put your task schedules.
Let's say you want to clean up old posts from your blog every day at midnight. You might define a console command called posts:cleanup
that performs this task. You would then schedule it like so:
<?php
use Illuminate\Foundation\Inspiring;
use Illuminate\Support\Facades\Artisan;
use Illuminate\Support\Facades\Schedule;
Artisan::command('inspire', function () {
$this->comment(Inspiring::quote());
})->purpose('Display an inspiring quote')->hourly();
Schedule::call(function (Schedule $schedule) {
$schedule->command('posts:cleanup');
})->daily();
Laravel's scheduler provides a variety of methods to specify when tasks should run, such as hourly
, daily
, weekly
, monthly
, and even everyMinute
for frequent tasks.
To create the posts:cleanup
command mentioned above, you would use the command:
php artisan make:command PostsCleanup
This command generates a new command class in the app/Console/Commands
directory. You can then define the logic for cleaning up old posts within the handle
method of the generated class:
<?php
namespace App\Console\Commands;
use Illuminate\Console\Command;
use App\Models\Post;
class PostsCleanup extends Command
{
protected $signature = 'posts:cleanup';
protected $description = 'Cleans up old posts from the blog';
public function handle()
{
// Delete posts older than a year
$deletedPosts = Post::where('created_at', '<', now()->subYear() )
->delete();
$this->info($deletedPosts .' old posts have been cleaned up!');
}
}
You can also run the same command from the terminal by entering:
php artisan posts:cleanup
Laravel's task scheduling is a powerful feature that can significantly reduce the manual effort required to maintain your application. By automating routine tasks, you ensure that your application is not only up-to-date but also performing optimally.
This article is part of our series Laravel 11 for Beginners: A Step-by-Step Guide to Learn the Concepts.
If you need help with a Laravel project let's get in touch.
Lucky Media is proud to be recognized as a Top Laravel Development Agency
Technologies:
Related Posts
Stay up to date
Be updated with all news, products and tips we share!