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
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.