In this guide, I’ll show you how to schedule and run cron jobs in Laravel 12. You can automate tasks like emails, reports, or cleanup using Laravel’s built-in task scheduler. No more relying on browser hits or external scripts.
What is Laravel Task Scheduling?
Laravel’s task scheduler lets you automate repetitive tasks by writing scheduled commands in code. It replaces the need for messy cron syntax.
Step-by-Step: Set Up Cron Jobs in Laravel 12
Install laravel 12 application using the following command.
laravel new example-app
Run the following Artisan command to create a new console command:
php artisan make:command SendEmailReport --command=report:send
This will create a file in app/Console/Commands/SendEmailReport.php
namespace App\Console\Commands;
use Illuminate\Console\Command;
class SendEmailReport extends Command
{
protected $signature = 'report:send';
protected $description = 'Send daily email report';
public function handle()
{
// Your logic here
\Log::info("Daily email report sent at " . now());
// Or trigger a Mailable or job here
}
}
routes/console.php
<?php
use Illuminate\Support\Facades\Schedule;
Schedule::command('report:send')->everyMinute();
You can customize the timing using methods like:
->everyMinute()
->hourly()
->daily()
->weekly()
->cron('0 0 * * *')
// custom cron expression
To test your command manually.
php artisan report:send
To test the scheduler manually.
php artisan schedule:run
Open your crontab editor:
crontab -e
Add the following line:
* * * * * cd /path-to-your-project && php artisan schedule:run >> /dev/null 2>&1
Replace /path-to-your-project with your Laravel project directory.
This runs Laravel's scheduler every minute, and Laravel will decide which tasks should run.
You might also like: