There is an Internet shop. In the database there are the tables:
Categories; Subcategories; Products; Product_images;
Product_images stores the paths to the images.
How to implement the deletion of the actual images when deleting a product.
Now set up using Foreign keys deleting a category automatically deletes its subcategories.
For the subcategory to be deleted its products, and for products to be removed and the information about images from database.
Example:
$table->foreign('product_id')->references('id')->on('products')->onDelete('cascade');
Whether it is necessary to use the foreign keys and cascade in sql to auto delete? Or maybe it's already implemented in the code, i.e. When you delete a category, first select and get all the sub categories, then in each cycle to remove in order to trigger the deleting and deleted events, which in turn will do the same with their products, which, accordingly with the images.
(
https://github.com/laravel/framework/issues/2536 - on call event delete*)
So in order will be called the deleted events and you can easily add any of their actions, such as physical destruction of images.
It seems to me that having configured foreign keys to pass around this in the code extra - stupid. So how can I move then deleting it in code and do only them.
Would not it be a crutch that the uninstall option that I suggested above?
I at first was confused, because it will add to the cycle in the destroy method of each model in this structure. However, as I understand the decision of using foreign keys is not so easy to extend to the desired functionality.
The project is completely academic. with the aim of self-development and the development of new technologies.