I
I
Ivan2021-11-10 18:54:08
Laravel
Ivan, 2021-11-10 18:54:08

In Laravel, you can somehow delete the model, but with the removal of all linking records?

For example, I want to delete a user $user->delete(), but in several other places there is a user_id key. So that it deletes the entries with these keys. And those, in turn, can pull something else. To remove all of this. Tried
->forceDelete(), doesn't help.

Answer the question

In order to leave comments, you need to log in

2 answer(s)
W
WebDev, 2021-11-10
@kirill-93

If foreign keys are installed, then it will drop everything by itself. If not set, then first delete the related entities, then the user itself.

J
jazzus, 2021-11-10
@jazzus

in migrations, you need to add a cascade delete to the keys https://laravel.com/docs/8.x/migrations#foreign-ke...
or delete the deleted event in the model observer
https://laravel.com/docs/8.x /eloquent#observers

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question