Laravel 5: Dispatching Queued Jobs With dispatch

April 14, 2018 —John Koster

The dispatch helper function can be used to push a new job onto the job queue (or dispatch the job). This function returns a new instance of "\Illuminate\Foundation\Bus\PendingDispatch"; having access to this class instance will allow you to modify properties about the pending job operation, such as its priority level.

#Signature

The signature of the dispatch function:

1function dispatch(
2 $job
3);

#Example Use

Assuming a variable $job exists that contains a valid queueable Job the following examples are all equivalent:

1use Illuminate\Contracts\Bus\Dispatcher;
2use Illuminate\Support\Facades\Bus;
3 
4dispatch($job);
5app(Dispatcher::class)->dispatch($job);
6Bus::dispatch($job);

Since the dispatch function returns and instance of PendingDispatch, we can make adjustments to some of the job's properties before it is pushed into the job queue:

1// Dispatch the job to a higher priority queue.
2dispatch($job)->onQueue('high');

Some absolutely amazing
people

The following amazing people help support this site and my open source projects ♥️
If you're interesting in supporting my work and want to show up on this list, check out my GitHub Sponsors Profile.