Laravel 11 / Foundation\Bus PendingChain::catchCallbacks()

laravel 11 pendingchain catchcallbacks
07 April 2025

Familiarity with Using PendingChain in Laravel


Hello friends! Today we want to talk about using PendingChain in Laravel 11. You may have worked with Job instances in Laravel and you might know how much they can assist in asynchronous processing and task management. Now imagine that you can perform several tasks in a chain, meaning that each task starts only after the previous one has completed. This is where PendingChain comes into play.


By using PendingChain, you can chain multiple tasks together and access the results of this chain easily with catchCallbacks. This feature allows you to check whether your tasks were executed successfully or not and if there were any errors, what actions should be taken.


In this article, we will delve into catchCallbacks and provide a simple example of how to use it in Laravel 11. We will also take a look at how to register callbacks for successful and unsuccessful tasks. This will give you the capability to execute different actions based on the outcomes of your tasks.


How to Use PendingChain


In this section, we will review a few lines of code to see how we can use PendingChain and catchCallbacks. Stick with me!


$chain = Bus::chain([
new FirstJob(),
new SecondJob(),
])->catchCallbacks(function () {
// Task execution in case of error
})->dispatch();

Code Explanation


In the first line, we are using the chain method to create a chain of tasks. We add two Job instances named FirstJob and SecondJob to the chain.


In the second line, we refer to catchCallbacks, which is a method that allows us to define a callback that will be executed if any of the tasks encounter an error. In this case, you can register tasks like logging the error or sending an email to the system administrator.


Finally, by invoking dispatch, this chain of tasks will be processed. Keep in mind that this method executes tasks sequentially.


FAQ

?

What is PendingChain in Laravel?

?

How can I use catchCallbacks?

?

What is the difference between Job and PendingChain?