Courses

Queues in Laravel 12

Method failed(): Inform Admin About Failure and Perform Cleanup

You're reading a FREE PREVIEW of a PREMIUM course.
Summary of this lesson:
- Implementing the failed() method in job classes to handle failures gracefully
- Using the method to notify administrators about job failures
- Creating custom logs or notifications when jobs fail
- Performing cleanup operations when a job fails (e.g., deleting records)

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...

The full lesson is only for Premium Members.
Want to access all 15 lessons of this course? (46 min read)

You also get:

  • 76 courses
  • Premium tutorials
  • Access to repositories
  • Private Discord