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?