Matt Larsuma
Matt Larsuma

Reputation: 1519

Dispatching a Laravel job in scheduler, pre-5.5

I'm used to Laravel 5.5+ where you can call $schedule->job(new ExampleJob); to fire jobs, which is not available in 5.4. I'm attempting to do something like this:

$schedule->call(function () {
    dispatch(new AppointmentReminder);
})->dailyAt('08:00');

but the job is not firing. I've verified that this is being called at the correct time. I'm guessing the dispatch() method is not available in App\Console\Kernal.php? Does anyone know of the official way of dispatching jobs in 5.4's scheduler? This is a legacy code base and all of the jobs are inline in the Kernal.php which is a total mess. Not to mention this is a rather involved job.

I did try use Illuminate\Foundation\Bus\DispatchesJobs;/use DispatchesJobs; and then $this->dispatch(new AppointmentReminder()); in Kernal.php, but that did not seem to do the trick either. Also, (new AppointmentReminder())->dispatch(); does not work. Thanks!

Upvotes: 0

Views: 1389

Answers (1)

Amirsadjad
Amirsadjad

Reputation: 515

You can create a new console command:

php artisan make:command CommandName

add it to App\Console\Kernal.php:

protected $commands = [
Commands\CommandName::class,      
];

and make you schedual call it:

$schedule->command('CommandName')->dailyAt('08:00');

and inside your command in the "handle" function, dispatch the job.

Upvotes: 1

Related Questions