例如,如果我有表格:
project
user_project
我想从项目表中删除一些项目以及user_project表中与该项目相关的所有记录,我该怎么做?我可以在单行中做到这一点吗?因为我不想让某些项目被破坏,但user_project仍然存在......
如果在迁移中设置了删除级联,则删除项目表中的记录将自动从相关表中删除该记录,否则必须手动删除它。
// to delete records from both table, let first delete from the
// child table then after that we delete from the parent table.
$project = Project::find($id);
// let assume you have define the user relation in your model and
// its a many to many association
$project->users()->detach();
$project->delete();