【发布时间】:2014-11-20 01:03:28
【问题描述】:
在我的问答项目中,管理员可以查看所有问题并根据需要删除每个问题。但一个问题 id 有 5 个相关表。也就是说,每个问题 id 都存在于 db 的 5 个表中,当管理员删除一个问题时,我必须根据这个问题 id 从这 5 个表中删除所有条目。
在我的控制器中
DB::table('user_questions')->where('question_id','=',$question_id)->delete();
DB::table('masters_answers')->where('question_id','=',$question_id)->delete();
DB::table('masters_questions')->where('question_id','=',$question_id)->delete();
DB::table('question_to_category')->where('question_id','=',$question_id)->delete();
DB::table('question_to_tags')->where('question_id','=',$question_id)->delete();
return Redirect::action('AdminController@anyShowQuestions', array());
上面的代码可以工作,但是有什么方法可以在 laravel 的一个 db 查询中执行相同的过程。?我已经提到了这个post,但找不到适合我的解决方案。如果有人提出正确的方法会很有帮助??
【问题讨论】:
-
由于记录都是相关的,您可以设置外键约束并将
ON DELETE设置为CASCADE
标签: php mysql laravel-4 delete-row