Laravel's task scheduling feature lets developers automate repeating tasks. It uses the built-in scheduler, removing the need for a separate cron file. It makes it easier to schedule tasks such as log cleaning, report sending, and data synchronization.
The scheduler was added to Laravel to provide a programmatic interface for handling cron jobs, allowing developers to specify all tasks within the application.
Define a task to clean logs daily in routes/console.php
:
use Illuminate\Support\Facades\Schedule;
Schedule::command('logs:clear')->daily();
Then create the command:
php artisan make:command ClearLogs
In the generated command class:
namespace App\Console\Commands;
class ClearLogs extends Command
{
protected $signature = 'logs:clear';
public function handle()
{
// Logic to clear logs
}
}