F
F
fomenko_alexandr2017-05-28 23:07:55
MySQL
fomenko_alexandr, 2017-05-28 23:07:55

How to delete related records (laravel)?

Hello masters.
Please tell me how to delete related records in laravel, despite the fact that the data in the database is related.
Here, for example, there are several tables: categories, works, screenshots of works
How to make sure that if we delete a category, then all entries are deleted: works, screenshots.
When I try to delete just a directory, I get an error: "Cannot delete or update a parent row: a foreign key constraint fails"

Answer the question

In order to leave comments, you need to log in

1 answer(s)
F
fomenko_alexandr, 2017-05-28
@fomenko_alexandr

Solved the problem thanks to Eugene @Wolfnsex
The solution was simple: in relation to tables, when updating and deleting, the option was changed to "cascade"
Answer:
As far as I understand, in this case, we have "works, screenshots" tied to a category? The right word, do not torment Lara, for this, the capabilities of the database itself are more than enough. Read more here (or a picture from HeidiSQL). When creating a foreign key, we indicate that when deleting a parent record referenced by a child (dependent) record in this table, it will also be deleted, following the "parent", just like all other records that refer to the parent record to be deleted.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question