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 soft deleting trait, calling delete sets the exists property to false, and as such a subsequent call to forceDelete does not actually force delete the model. Moreover, this causes some inconsistency with the exists property - immediately after soft-deleting it's false but if you fetch a soft-deleted model from the database it's true.
Steps To Reproduce:
Create a model with the soft deleting trait.
Save a new record of that model.
Call delete on the model.
Call forceDelete on the model.
At this point the soft-deleted record is still in the database (expected result is for it to be deleted).
Example
This is using a fresh copy of the basic Laravel project with the User model modified to use soft deletes.
App\User::withTrashed()->count(); // 0, as expectedApp\User::create(['name' => 'test', 'email' => 'test', 'password' => 'test']);
App\User::withTrashed()->count(); // 1, as expected$user = App\User::withTrashed()->first();
$user->exists; // true, as expected$user->trashed(); // false, as expected$user->delete();
App\User::withTrashed()->count(); // 1, as expected$user->exists; // false$user->trashed(); // true, as expected$user->forceDelete();
App\User::withTrashed()->count(); // 1, record was not actually force deleted, should be 0$user = App\User::withTrashed()->first();
$user->exists; // true, inconsistent with the false right after calling delete()$user->trashed(); // true, as expected$user->forceDelete();
App\User::withTrashed()->count(); // 0, as expected
The text was updated successfully, but these errors were encountered:
I'm having this same issue. To provide some additional insight, when calling the forceDelete() method on the user without having soft deleted first, just puts it in a soft-deleted state.
Looking into the code for the Model class we find that forceDelete() simply calls delete(). There's no additional logic.
/**
* Force a hard delete on a soft deleted model.
*
* This method protects developers from running forceDelete when trait is missing.
*
* @return bool|null
*/
public function forceDelete()
{
return $this->delete();
}
Description:
When using the soft deleting trait, calling
delete
sets theexists
property tofalse
, and as such a subsequent call toforceDelete
does not actually force delete the model. Moreover, this causes some inconsistency with theexists
property - immediately after soft-deleting it'sfalse
but if you fetch a soft-deleted model from the database it'strue
.Steps To Reproduce:
delete
on the model.forceDelete
on the model.At this point the soft-deleted record is still in the database (expected result is for it to be deleted).
Example
This is using a fresh copy of the basic Laravel project with the User model modified to use soft deletes.
The text was updated successfully, but these errors were encountered: