Home > Software engineering >  Customize cron runtime in laravel?
Customize cron runtime in laravel?

Time:01-03

I have a file Commands/DateUpdate.php

protected $signature = 'date:update';
public function handle()
{
    $listSchedule = Schedule::where('dateSchedule', '=', strtotime('now'))->get()->toArray();
    $dateSchedule = [];
    foreach ($listSchedule as $value) {
        $dateSchedule[] = $value['dateSchedule'];
    } 
    var_dump($dateSchedule);
    // result
    // array(3) {
    //     [0]=>
    //     string(16) "2023-01-05 09:16"
    //     [1]=>
    //     string(16) "2023-01-05 11:45"
    //     [2]=>
    //     string(16) "2023-01-05 15:25"
    //   }

}

In Kernel.php $schedule->command('date:update'); I want to schedule it according to the time in the $dateSchedule variable. Example:

foreach ($dateSchedule as $item) {
    ->at($item);
};

How can this be done?

CodePudding user response:

You can use

foreach ($dateSchedule as $item) {
    $schedule->call(function () {
        $this->call('date:update');
    })->at($item);
};

CodePudding user response:

You can try by compare current time with dateSchedule

foreach ($listSchedule as $value) {
  if (now()->diffInMinutes($value['dateSchedule']) === 0) {
    $this->call('another:command');
  }
}
  • Related