Arvin Flores
Arvin Flores

Reputation: 487

How to fire subsequent job in Laravel 5.2

I would like to push another job onto the queue after my first one has successfully executed. From my ShopController.php I fire:

$this->dispatch(new ItemPurchased($myVariables));

In ItemPurchased.php:

public function handle()
{
  // some code that charges a user's credit card
}

How do i fire a subsequent job upon success of ItemPurchased.php?

From the Laravel Documentation, you would do something like this in a ServiceProvider:

{
    Queue::after(function (JobProcessed $event) {
        // $event->connectionName
        // $event->job
        // $event->data
    });
}

But how do i specify, after ItemPurchase.php was successfuly, then dispatch another job? There is a failed() method for code to run when jobs fail. Is there a success() method that hasn't been mentioned before?

Upvotes: 0

Views: 729

Answers (1)

Mina Abadir
Mina Abadir

Reputation: 2981

Jobs run offline through the Queue driver. However, once the handle method is called it follows the normal sync code execution.

So simply at the end of handle method just fire the new job:

public function handle()
{
    //Some code here
    dispatch(new JobClass());
}

Upvotes: 1

Related Questions