Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Force delete does not force delete immediately after model is soft deleted #18808

Closed
vetruvet opened this issue Apr 14, 2017 · 2 comments
Closed

Comments

@vetruvet
Copy link
Contributor

Description:

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:

  1. Create a model with the soft deleting trait.
  2. Save a new record of that model.
  3. Call delete on the model.
  4. 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 expected
App\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
@luisten
Copy link

luisten commented May 10, 2017

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();
    }

I'm not sure if I'm missing something here.

@themsaid
Copy link
Member

This was fixed in #17613 for version 5.5

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants