問題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();