Laravel - Execute queued job automatically

user3242861 picture user3242861 · Jun 21, 2018 · Viewed 9.7k times · Source

I have jobs to send several emails.

In my controller I call the job:

dispatch(new SendStartPatEmail($data));

And record is saved in table jobs.

But to execute the job I have to run php artisan queued:work manually. How can I do this automatically?

Answer

Jonathon picture Jonathon · Jun 21, 2018

There are lots of different ways, all depending on the environment that you're using. Laravel tends to recommend using Supervisor to monitor your queue workers and keep them running.

Alternatively, you may wish to have your jobs execute immediately, instead of adding them to a queue. You can do this by setting your queue driver to sync, either in your config:

config/queue.php

'default' => env('QUEUE_DRIVER', 'sync'),

or in your .env file (assuming your config is set up as above)

.env

QUEUE_DRIVER=sync