#Soft Delete and then run the job, works. Hard Delete and then run the job, fails.

5 messages · Page 1 of 1 (latest)

clever pagoda
#

I've encountered an issue in my Laravel application, and I'm seeking clarification on the behavior of soft deletes in Laravel 8.50.

Here's what I did initially:

Dispatch(Job to perform before deletion for a particular user);
$user->delete();

In this scenario, everything works as expected. The job executes successfully because it can access the restoreModel function within the SerializesAndRestoresModelIdentifiers.php file:

public function restoreModel($value)
    {
        return $this->getQueryForModelRestoration(
            (new $value->class)->setConnection($value->connection), $value->id
        )->useWritePdo()->firstOrFail()->load($value->relations ?? []);
    }

However, when I tried the following:

Dispatch(Job to perform before deletion for a particular user);
$user->forceDelete();

The job fails due to a ModelNotFoundException.

My confusion arises from the fact that the Laravel documentation and online sources seem to suggest that soft deletes should indeed fail when trying to access deleted items. In my case, soft deleting the user works, and hard deleting (force deleting) doesn't, which is contrary to my expectations. My job should fail in BOTH the cases.

I am using Laravel version 8.50. Is this behavior a bug in Laravel, or is there a specific configuration or aspect of soft deletes that I might be missing?

stuck thorn
#

I think the job would serialize models, saving only the ID, then it fetches the job when it's to be processed, simply doing a where id = x query. No clue what this has to do with restoring models, because Laravel wouldn't do that on it's on when it's just processing jobs.
Your code doesn't make much sense, because you dispatch a job, then right after you delete the user, and you probably are accessing the user in the job. Since a job is asynchronous, the user would've been deleted before the job is running, if you force delete it, then it won't ever exist

clever pagoda
stuck thorn
#

I don't think it does, but I'm not sure actually. Laravel simply serializes the models, which only stores the ID of the model, then when the job is processed the model is fetched again. I'd assume that would also work with trashed models

clever pagoda
#

Yea, and it seems like it. When I logged restoreModel , my soft deleted user does return the data. Nevertheless, the articles from google suggest otherwise.