Scheduling a repetitive task is a great way of saving time. Often we might need to schedule a task to – say sending an email daily at a given time to all the existing users. Manually if we try to achieve this; it will become impossible once the user base crosses a certain threshold. Luckily for us, Laravel comes with its own Task Scheduler. Follow the tutorial to learn how to schedule a task in Laravel.
Step 1: Open your existing Laravel project or create a new one first. Navigate to its location and create a New Cron Command.
php artisan make:command TransactionRecordCron --command=transactionrecord:cron
Step 2: Navigate to app/Console/Commands/TransactionRecordCron.php
<?php
namespace App\Console\Commands;
use Illuminate\Console\Command;
use Log;
class TransactionRecordCron extends Command
{
/**
* The name and signature of the console command.
*
* @var string
*/
protected $signature = 'transactionrecord:cron';
/**
* The console command description.
*
* @var string
*/
protected $description = 'Command description';
/**
* Create a new command instance.
*
* @return void
*/
public function __construct()
{
parent::__construct();
}
/**
* Execute the console command.
*
* @return int
*/
public function handle()
{
Log::info(__CLASS__."::".__FUNCTION__." Start");
//Insert Your Code/Logic here
Log::info(__CLASS__."::".__FUNCTION__." End");
}
}
Step 3: Schedule your Cron Command in app/Console/Kernel.php
<?php
namespace App\Console;
use Illuminate\Console\Scheduling\Schedule;
use Illuminate\Foundation\Console\Kernel as ConsoleKernel;
class Kernel extends ConsoleKernel
{
/**
* The Artisan commands provided by your application.
*
* @var array
*/
protected $commands = [
Commands\TransactionRecordCron::class,
//
];
/**
* Define the application's command schedule.
*
* @param \Illuminate\Console\Scheduling\Schedule $schedule
* @return void
*/
protected function schedule(Schedule $schedule)
{
// $schedule->command('inspire')->hourly();
$schedule->command('transactionrecord:cron')
->everyMinute();
}
/**
* Register the commands for the application.
*
* @return void
*/
protected function commands()
{
$this->load(__DIR__.'/Commands');
require base_path('routes/console.php');
}
}
Step 4 (Testing Only): Do a trial run
php artisan schedule:run
Step 6: Start the Scheduler
* * * * * cd /path-to-your-project && php artisan schedule:run >> /dev/null 2>&1
Debugging Note:
- Check Error Log: Debugging check laravel.log for errors storage/logs/laravel.php
- Set Timezone: Open config\app.php and edit ‘timezone’ => ‘UTC’
/*
|--------------------------------------------------------------------------
| Application Timezone
|--------------------------------------------------------------------------
|
| Here you may specify the default timezone for your application, which
| will be used by the PHP date and date-time functions. We have gone
| ahead and set this to a sensible default for you out of the box.
|
*/
/*'timezone' => 'UTC',*/
'timezone' => 'Asia/Kolkata',
Md. Saifur Rahman is a Full Stack Django and Laravel Developer. Additionally, he loves to spend time learning and researching about the Internet of Things (IoT). He loves to share his work and contribute to helping fellow developers.