The scheduler helps you run tasks automatically at set times without doing it yourself.
Scheduler with cron in Laravel
protected function schedule(Schedule $schedule)
{
$schedule->command('your:command')->cron('* * * * *');
}The cron method uses standard cron syntax: minute, hour, day, month, weekday.
Put your scheduled commands inside the schedule method in app/Console/Kernel.php.
emails:send command every day at 9:00 AM.$schedule->command('emails:send')->cron('0 9 * * *');
backup:run command every Sunday at midnight.$schedule->command('backup:run')->cron('0 0 * * 0');
cleanup:temp command every 15 minutes.$schedule->command('cleanup:temp')->cron('*/15 * * * *');
This example shows how to schedule a command named report:generate to run every day at 7 AM using cron syntax. The command prints a message when it runs.
<?php namespace AppConsole; use IlluminateConsoleSchedulingSchedule; use IlluminateFoundationConsoleKernel as ConsoleKernel; class Kernel extends ConsoleKernel { protected function schedule(Schedule $schedule): void { // Run the 'report:generate' command every day at 7 AM $schedule->command('report:generate')->cron('0 7 * * *'); } protected function commands(): void { $this->load(__DIR__.'/Commands'); } } // Example command class namespace AppConsoleCommands; use IlluminateConsoleCommand; class ReportGenerate extends Command { protected $signature = 'report:generate'; protected $description = 'Generate daily report'; public function handle(): int { // Simulate report generation $this->info('Report generated successfully.'); return 0; } }
Make sure your server runs the Laravel scheduler every minute by adding this cron entry: * * * * * php /path-to-your-project/artisan schedule:run >> /dev/null 2>&1
Use php artisan schedule:list to see all scheduled tasks.
Test your commands manually with php artisan your:command before scheduling.
The scheduler runs tasks automatically using cron timing.
Define scheduled commands inside app/Console/Kernel.php.
Set up a system cron job to trigger Laravel's scheduler every minute.