Skip to content

Restore fails on tree descendants #103

@thijskok

Description

@thijskok

When a restore is called on a deleted root node, restoreDescendants is called to restore the children as well. The method looks like this:

$this->descendants()
     ->where($this->getDeletedAtColumn(), '>=', $deletedAt)
     ->applyScopes()
     ->restore();

The applyScopes method is called, which adds the soft-delete global scope, causing the query to look like this:

update `tree` 
set `deleted_at` = '', `updated_at` = '2016-04-14 10:54:08' 
where `_lft` between '2' and '10' 
and `deleted_at` >= '2016-04-14 10:54:00' 
and `tree`.`deleted_at` is null

The two deleted_at conditions are in conflict, which causes the descendants not to restore. The solution would be to add the withTrashed method, avoiding the soft-delete global scope:

$this->descendants()
     ->where($this->getDeletedAtColumn(), '>=', $deletedAt)
     ->withTrashed()
     ->applyScopes()
     ->restore();

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions