W
W
WebDev2017-03-08 17:05:38
Laravel
WebDev, 2017-03-08 17:05:38

The inconvenience of laravel soft-delete?

I use "soft delete" in laravel, that is, instead of deleting entries, they are marked as deleted.
I have the following structure:
- Store
-- Section
--- Catalog
---- Product
And there is a table of orders separately, where there is a field product_id
Now imagine that the user ordered the product, and after that I deleted the store. If you do not use soft deletion, then the section, catalog, product and order with this product will be deleted through foreign keys and everything will be fine, but only the store is marked as deleted and in the order controller I successfully see an order with a deleted product:
Order::all();
It turns out that I need to either check the whole chain through join, for example:

Order::join('products...')->join('catalogs...')->join('shop...')->whereNull('products.deleted_at')->whereNull('catalogs.deleted_at') и тп

Or, when deleting, update the deleted_at field for all child entities.
How do you work with it?

Answer the question

In order to leave comments, you need to log in

2 answer(s)
S
Stanislav Pochepko, 2017-03-08
@kirill-93

I made a selection of the necessary related models in the event handler for deleted on the model and also deleted them.

A
ajaxtelamonid, 2017-03-09
@ajaxtelamonid

Maybe I'm a retrograde, but I think foreign keys are evil. The business logic is sent to an untested remote place that you can forget about and one day screw up.
Only explicitly in code, only hardcore.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question