How to push jobs to the queue in Laravel (Cheat sheet)

Categorized as Laravel

Here is the cheat sheet on the ways you can push jobs to queue in your Laravel application:

// Push a job to the queue
Queue::push(new FulfilOrder($order));

dispatch(new FulfilOrder($order));

(new FulfilOrder($order))->dispatch;

// Push the job after a given number of seconds
Queue::later(15, new FulfilOrder($order));

dispatch(new FulfilOrder($order))->delay(15);

(new FulfilOrder($order))->dispatch()->delay(15);

// Push on specific queue
Queue::pushOn('orders', new FulfilOrder($order));

dispatch(new FulfilOrder($order))->onQueue('orders');

(new FulfilOrder($order))->dispatch()->onQueue('orders');


// Dispatch conditionally
FulfilOrder::dispatchIf($order->isPaid(), $order);

FulfilOrder::dispatchUnless($order->isCanceled(), $order);

// Push on specific queue with time delay
Queue::laterOn('orders', 15, new FulfilOrder($order));

// Push multiple jobs
  new FulfilOrder($order),
  new ThankYouEmail($order)

// Push multiple to specific queue
  new FulfilOrder($order),
  new ThankYouEmail($order)
], null, 'orders');

If you are configuring Laravel Queues with AWS SQS, and you want to queue a job later than 15 minutes in the future, you may want to use the Laravel Haystack package.

You can also use the Bus::chain method

use App\Jobs\OptimizePodcast;
use App\Jobs\ProcessPodcast;
use App\Jobs\ReleasePodcast;
use Illuminate\Support\Facades\Bus;
    new ProcessPodcast,
    new OptimizePodcast,
    new ReleasePodcast,

with chain closures:

    new ProcessPodcast,
    new OptimizePodcast,
    function () {
        Podcast::update(/* ... */);

Via @cosmeescobedo.

Leave a reply

Your email address will not be published. Required fields are marked *