问题hasmany答案。
解决方法hasmany评论。
评论hasmany Votes。
答案hasmany票数。删除所有关系表中的记录
现在,如果我删除了一个问题,我想问题以及它的答案,并且所有评论和答案上的投票都将被删除。我做了什么看起来不那么优雅。
$question=Question::where(['id'=>$request->id])->first();
$answers=Answer::where(['q_id'=>$request->id])->get();
foreach($answers as $answer)
{
$comments=Comment::where(['answer_id'=>$answer->id])->get();
foreach($comments as $comment)
{
$votes=vote::where(['votable_id'=>$answer->id])
->orWhere(['votable_id'=>$comment->id])
->get();
foreach($votes as $vote)
{
$vote->delete();
}
$comment->delete();
}
$answer->delete();
}
$question->delete();