As when you delete the article deleting the related table field pictures and further ACC. files pictures?

In General, there are three tables with these fields:
posts: id, name, created_at,...
imageables: imageable_id, imageable_type, file_id, title,...
files: id, name, path, type, is_used...

also configured foreign key when deleting the file (tab. files) - removes a field from the table imageables

Each article is associated with multiple symbols (polymorphic relationship), with each image associated with one file.

Need deleting article to remove all related models of the images (in table imageables) + for the associated file (imageables) to set a field is_used = 0 (the file is then cleared for the crown for example...)

I want the next team it was all done by machine:
\Post::find(1)->fieldImages()->delete()
and there was no need at all to follow table files
June 10th 19 at 16:50
2 answers
June 10th 19 at 16:52
Solution
Foreign keys has nothing to do with. And help observery.
Thank you. Will use observera: if you delete an article will delete records pictures.

But still arose the next question, just for fun: if, for example, delete a lot of pictures in this way:
\Post::find(1)->images()->delete()

as if we don't work with the model a single entity in the Image, respectively. if I was the observer for the event deleted entity Image, it wouldn't work... then how to fix event collection? - Vince89 commented on June 10th 19 at 16:55
in the above code, you do not work with the model Image and not "as if". And collection nothing to do with it. It bilder.
Will help something like \Post::find(1)->images->each->delete(), read here. - Penelope91 commented on June 10th 19 at 16:58
June 10th 19 at 16:54
Solution
Keys can safely remove relations, but the files - read the documentation https://laravel.com/docs/5.5/eloquent#observers it's about the events in the models. Create observer and describe the deletion of files.
files will not delete immediately, just in the fact table there is a field that is not in use and can be removed. Removed, maybe will be the crown, or something, that is not important.
And for observer thank you. Previously knew about the events of the models, but as observer missed - Vince89 commented on June 10th 19 at 16:57

Find more questions by tags Laravel