Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Laravel 5.2: How to scheduled Command multiple times in a day

I am using laravel 5.2 commands via scheduler, i can call the command by following code:

$schedule->command('command:name')
                ->dailyAt('08:55');

but now i want to call above command daily at six different times i.e. 8:45, 9:15, 9:45,10:15 etc

$schedule->command('command:name')
            ->when(function(){return true;});

Above code, with when function, isn't working somehow, can someone suggest best practices of laravel.

like image 969
Awn Ali Avatar asked Jan 22 '17 10:01

Awn Ali


1 Answers

Why not just define 4 tasks, it's simple and readable:

$schedule->command('command:name')->dailyAt('08:55');
$schedule->command('command:name')->dailyAt('09:15');
$schedule->command('command:name')->dailyAt('09:45');
$schedule->command('command:name')->dailyAt('10:15');

Also, you can put it in a loop:

foreach (['08:45', '09:15', '09:45', '10:15'] as $time) {
    $schedule->command('command:name')->dailyAt($time);
}
like image 182
Alexey Mezenin Avatar answered Nov 07 '22 08:11

Alexey Mezenin