How to implement deleting images, deleting a product from the store?


Warning: count(): Parameter must be an array or an object that implements Countable in /home/styllloz/public_html/qa-theme/donut-theme/qa-donut-layer.php on line 274
0 like 0 dislike
20 views
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.
by | 20 views

2 Answers

0 like 0 dislike
Well, the connection to the database will not allow You to remove the image, so you need to consider the logic in the application itself.

PS If you are using MySQL - please note:
\rWhy not fire the trigger MYSQL?
by
0 like 0 dislike
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.

If you want to hang some logic on event deletion - only. As the alternative is to do a soft delete when you delete a category, it will be faster. And if you are in the database these records are not necessary - it is possible for the crown to have physically delete all soft deleted categories/subcategories/products.
by

Related questions

0 like 0 dislike
1 answer
0 like 0 dislike
4 answers
0 like 0 dislike
1 answer
asked Apr 27, 2019 by rusgayfer
0 like 0 dislike
2 answers
0 like 0 dislike
4 answers
110,608 questions
257,187 answers
0 comments
40,796 users