In the previous lessons, we've learned how to set up and use Laravel queues. Now, let's explore what happens when jobs fail and how we can handle these failures properly using the failed() method.
Implementing the failed() Method
You can define a failed()
method directly in your job class. This method will be automatically called by Laravel when the job fails to process.
Let's add a simple implementation to our SendRegisteredUserNotification
job:
use Throwable; class SendRegisteredUserNotification implements ShouldQueue{ // ... public function failed(?Throwable $exception): void { info('Failed to process notification: ' . get_class($exception) . ' - ' . $exception->getMessage()); }}
The failed()
method receives the exception that caused the job to fail as its parameter, giving you access to all the details about what went wrong.
How It Works in Practice
When a job fails and you have...