You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
When using the SoftDeletes trait, deleted models are ignored in Jobs which are using the $deleteWhenMissingModels = true feature, and the Jobs are still processed when they should be discarded.
The reason is that SoftDeletes trait is only taken into account when using Eloquent, but Jobs use the DB query builder to retrieve the serialized Models from the database, without checking the possible deleted_at column:
One possible way to solve it is to overwrite the newQueryForRestoration from Models on the SoftDeletes trait, making it check that the deleted_at column is null:
/** * Get a new query to restore one or more models by their queueable IDs. * * @param array|int $ids * @return \Illuminate\Database\Eloquent\Builder */publicfunctionnewQueryForRestoration($ids): Builder
{
return$this
->newQueryWithoutScopes()
->whereKey($ids)
->whereNull($this->getDeletedAtColumn());
}
As Laravel is an open source project, we rely on the community to help us diagnose and fix issues as it is not possible to research and fix every issue reported to us via GitHub.
If possible, please make a pull request fixing the issue you have described, along with corresponding tests. All pull requests are promptly reviewed by the Laravel team.
Laravel Version
10.3.2
PHP Version
8.2.5
Database Driver & Version
No response
Description
When using the SoftDeletes trait, deleted models are ignored in Jobs which are using the $deleteWhenMissingModels = true feature, and the Jobs are still processed when they should be discarded.
The reason is that SoftDeletes trait is only taken into account when using Eloquent, but Jobs use the DB query builder to retrieve the serialized Models from the database, without checking the possible deleted_at column:
framework/src/Illuminate/Queue/SerializesAndRestoresModelIdentifiers.php
Line 119 in d7616a1
framework/src/Illuminate/Database/Eloquent/Model.php
Line 1555 in d7616a1
Possible fix
One possible way to solve it is to overwrite the newQueryForRestoration from Models on the SoftDeletes trait, making it check that the deleted_at column is null:
Steps To Reproduce
If we create a Job injecting an ExampleModel, and then we delete the model and dispatch job:
The job should be automatically discarded, but it is processed anyway.
Repository demonstrating the issue
A minimal reproducible example is available here.
The example is dockerized. To set the docker container up, run:
docker-compose -up -d
Then, prepare the database running the needed migrations:
php artisan migrate
To test the issue:
php artisan test --filter=JobTest
This test should pass, but it fails, because the Job is handled instead of being discarded.
The text was updated successfully, but these errors were encountered: