#development #laravel #php

Laravel's queuing system is simply unique. It has so much power and functionality! When your application grows, you will soon start using a queue, and your queue will also get more and more jobs. It can happen that a model is deleted after queuing a job. If this happens, the job will fail and throw a ModelNotFoundException.

To prevent the job from ending up as a failed job, you can add the $deleteWhenMissingModels property to your job class. This also works for job chains. If the job has this property and fails, the rest of the chain will be stopped. There will be no failed job logged.

 1class ActivateCard implements ShouldQueue
 2{
 3    public $deleteWhenMissingModels = true;
 4
 5    private $card;
 6
 7    public function __construct(Card $card)
 8    {
 9        $this->card = $card;
10    }
11
12    public function handle()
13    {
14        // Do your thing here
15    }
16}

Keep in mind that you're careful with this approach. Let's say you have a system where you handle payments. In that case, you probably do want to know if something went wrong with generating a bill or managing some invoice.