Automatically deleting related rows in Laravel (Eloquent ORM)

后端 未结 13 1958
半阙折子戏
半阙折子戏 2020-11-22 17:03

When I delete a row using this syntax:

$user->delete();

Is there a way to attach a callback of sorts, so that it would e.g. do this auto

13条回答
  •  星月不相逢
    2020-11-22 17:35

    In my case it was pretty simple because my database tables are InnoDB with foreign keys with Cascade on Delete.

    So in this case if your photos table contains a foreign key reference for the user than all you have to do is to delete the hotel and the cleanup will be done by the Data Base, the data base will delete all the photos records from the data base.

提交回复
热议问题