r/laravel•
mcdonagg

Best way to handle Soft Delete and Restore with Cascade?

I am looking for any recommendations from on setting up a cascading soft delete with the ability to restore and have that cascade as well. So let’s say I have Projects which have many Requests which have many Comments. If I soft delete a Project I want that to side delete that and all of its Requests and Comments. I also want it to be able to Restore. So if I later restore that Project, it’s Requests and Comments will also be restored. With a twist I would also like it to not restore any Requests or Comments that were soft deleted manually prior to the Project deletion. Does anyone know of a package that would help with this, or an easy way to do this in Laravel 8 (too many times have I written something myself to find out later Laravel helps with that) or should I just code this up myself. Thank you so much for any help.

Posted Apr 5, 2021, 2:18 AM UTC

127
lukecorbett2389•

Look at model observers, pretty sure there’s a ‘restored’ event that you can use to then do anything you like. You can get the related models using withTrashed where the deleted_at time stamp is the same as the restored model.

6
rayronvictor•

In your model Project you can setup a boot function:

public static function boot()
{
    parent::boot();

    static::deleting(function ($project) {
        // delete
        foreach ($project->requests as $request) {
            $request->delete();
        }
        // delete
        foreach ($project->comments as $comment) {
            $comment->delete();
        }
    });

    static::restored(function ($project) {
        $project->requests()->restore()
        $project->comments()->restore()
    }
}
4
Affriction•

There's a package that does cascading soft deletes.

https://github.com/Askedio/laravel-soft-cascade

2
mcdonaggOP•

Thanks, that looks great, it looks like they haven’t solved the cascading restore that only restores those that were soft deleted by that parent and not on their own or by a different parent. But it may be a great place to start, thank you.

2
[deleted]•

That's imposible to solve with only soft deletes, if you think about it.

2
jammy-git•

Be careful with cascading soft deletes if you allow the related items to be deleted separately too.

For example, you have an order with order items. Customer decides to delete some items, which soft deletes them. Customer then cancels the order and admin person deletes the order, which cascade deletes all the items. Customer then decides they actually do want the order and admin person restores the order, however you've now lost track of which items were deleted separately and which were deleted along with the order. You can use timestamps for the soft delete flag, but it's not perfect.

I find it better to join the parent record into whichever queries and check the parent record delete_at field.

2
Tall-Act5727•

Generally we can delete all in cascade or we can delete only the strongest record and handle the other ones when reading, I see some options:

  1. Delete and restore cascade using model events, when you restore, just keep deleted relations that where deleted before the Project deleted_at(this approach can generate a lot of queries, its is bad for huge number of records)
  2. Delete only the Project, when you enter some route that needs requests or comments from the deleted Project you check if the project is deleted and throw a 404, this way you dont need cascade.
2
[deleted]•

[deleted]

1