Maaz Zindani

Loading the index…

↑↓ move↵ openesc close4072 pages
Node.js, PHP and APIs

A Laravel queued job gets the wrong data, or none at all

ErrorIlluminate\Database\Eloquent\ModelNotFoundException

Short answer

Laravel does not store a snapshot of a model you pass to a job. It stores the identifier and loads the row again when the worker runs. So a job can see newer data, fail because the row is gone, or run old code if the worker was never restarted. Check the failed-jobs list, restart workers, and dispatch after commit.

  • Applies to Laravel 12, PHP 8.3
  • Updated
  • By Maaz Zindani, Senior Software Engineer

Fix it fast

  1. 1Find out which cause you have

    List the failed jobs. A missing model shows up here with its exception, and an empty list points to the worker or a dropped dispatch instead.

    php artisan queue:failed
  2. 2Match what you see, then jump to its fix

    1. 1You added or changed a constructor argument, but the job behaves as if the old signature is in useThe worker is still running the old job classFix
    2. 2ModelNotFoundException in queue:failed, or a job that sees no records created just before dispatchThe model is deleted, or not yet saved, when the job runsFix
    3. 3The job sees updated column values, or a relationship is missing the filter you set before dispatchThe data in the job is newer, or less complete, than you expectedFix
    4. 4The first dispatch ran, but later dispatches with other arguments never appear on the queueA unique job silently ignores the new dispatchFix

What does the error look like?

  • Illuminate\Database\Eloquent\ModelNotFoundException
  • The job runs old code after a deploy
  • The job never runs when dispatched with the same arguments again

Cause 1 of 4

The worker is still running the old job class

1Check

A queue worker is a long-lived process. It keeps the application it booted with in memory, so it does not notice changed files until it restarts.

ps aux | grep "queue:work"

2Fix

Tell every worker to exit after its current job. Your process manager (Supervisor or similar) starts new ones that load the new code. Run this on every deploy.

php artisan queue:restart

3Verify

Dispatch the job again and watch the worker pick it up with the new arguments.

php artisan queue:work --once

Confirmed by laravel.com

Did this fix it?

Cause 2 of 4

The model is deleted, or not yet saved, when the job runs

1Check

A model given to the constructor is stored as its identifier and fetched again when the job is handled. If dispatch happens inside a database transaction, a worker can pick the job up before the transaction commits, so the row does not exist yet. A row deleted while the job waited gives the same error.

php artisan queue:failed
grep -rn "DB::transaction" app/

2Fix

Dispatch after the transaction commits. If deleted models are expected and the job is pointless without them, let Laravel drop the job quietly.

ProcessPodcast::dispatch($podcast)->afterCommit();

// inside the job class
public $deleteWhenMissingModels = true;

3Verify

Run the code path that dispatches the job and confirm nothing new appears in the failed list.

php artisan queue:failed

Confirmed by laravel.com

Did this fix it?

Cause 3 of 4

The data in the job is newer, or less complete, than you expected

1Check

Because the model is reloaded, values changed after dispatch are visible to the job. Relationship constraints applied before serialisation are not applied again. A collection or array of models also comes back without its loaded relationships.

grep -n "__construct" -A4 app/Jobs/ProcessPodcast.php

2Fix

If the job needs a fixed value, pass that value itself (an ID, a string, an array of scalars) instead of the model. If it needs a relationship, load it inside handle(). Encode binary data with base64_encode before passing it.

public function __construct(
    public int $podcastId,
    public string $format,
) {}

public function handle(): void
{
    $podcast = Podcast::with('episodes')->findOrFail($this->podcastId);
}

3Verify

Process one job and compare the values it logs with the values you dispatched.

php artisan queue:work --once

Confirmed by laravel.com

Did this fix it?

Cause 4 of 4

A unique job silently ignores the new dispatch

1Check

A job that implements ShouldBeUnique is not dispatched while another instance of it is on the queue and unfinished. The lock is based on uniqueId(), so different arguments with the same id are dropped.

grep -rn "ShouldBeUnique\|uniqueId" app/Jobs/

2Fix

Return an id that includes the argument that makes the job different, or remove the interface if the job should not be unique.

public function uniqueId(): string
{
    return $this->podcastId.'-'.$this->format;
}

3Verify

Dispatch twice with different arguments and run the worker until the queue is empty and confirm both jobs ran.

php artisan queue:work --stop-when-empty

Confirmed by laravel.com

Did this fix it?

How do I stop it coming back?

  • Run php artisan queue:restart as the last step of every deploy.
  • Pass plain values (IDs, strings, numbers) when the job needs a fixed input, and models only when current data is what you want.
  • Use afterCommit() when you dispatch inside a transaction.
  • Keep a look at queue:failed, or send failures to your error tracker, so a missing model is noticed on the first day.

Sources

How was this page made?

I drafted this page with AI assistance from the official documents listed under Sources. Every cause above names the document that confirms it. I have not reproduced this fix on a machine of my own yet, so read it as documented guidance, not a test report. Found a mistake? Tell me.

Which stack is behind this page?

Projects of mine built on the same technologies:

Related fixes

Back end

composer require maatwebsite/excel fails to install

Composer says the package needs an older PHP than the one installed

Composer refuses maatwebsite/excel when the PHP version or a PHP extension does not match what the package and PhpSpreadsheet need.

3 causes, each with a fix

Back end

composer require laravel/horizon fails: ext-pcntl is missing

laravel/horizon v1.1.0 requires ext-pcntl * -> the requested PHP extension pcntl is missing from your system

Horizon needs the pcntl extension, and Composer refuses to install it where PHP lacks that extension.

3 causes, each with a fix

Next step

Want this handled for good?

I look after servers, apps and releases end to end. Tell me what keeps breaking.