Perform Action when Job has failed

In some cases, we want to perform some action when job has failed. For example, send an email or a notification.

For this purpose, we can use failed() method in the job class, just like the handle() method:

1namespace App\Jobs\Invoice;
2use Illuminate\Bus\Batchable;
3use Illuminate\Bus\Queueable;
4use Illuminate\Queue\SerializesModels;
5use Illuminate\Queue\InteractsWithQueue;
6use Illuminate\Contracts\Queue\ShouldQueue;
7use Illuminate\Foundation\Bus\Dispatchable;
8 
9class CalculateSingleConsignment implements ShouldQueue
10{
11 use Batchable, Dispatchable, InteractsWithQueue, Queueable, SerializesModels;
12 
13 // ... __construct() method, handle() method, etc.
14 
15 public function failed()
16 {
17 // Perfom any action here when job has failed
18 }
19}

Tip given by @pauloimon

Like our articles?

Become a Premium Member for $129/year or $29/month
What else you will get:
  • 22 courses (477 lessons, total 38 h 20 min)
  • 2 long-form tutorials (one new every week)
  • access to project repositories
  • access to private Discord