我想从评论表中删除所有评论。我这样做是这样做的,但这不是 laravel 的方式。请任何人回答正确的步骤。
$comments = Comment::where('post_id',$id)->get();
if(count($comments)>1)
{
$comment_id= [];
foreach ($comments as $i)
{
$comment_id[] = $i->id;
}
for($i=0;$i<count($comments);$i++)
{
Comment::find($comment_id[$i])->delete();
}
}
elseif (count($comments)==1)
{
$comments->delete();
}
Run Code Online (Sandbox Code Playgroud)
Evg*_*hev 16
由于每个 Eloquent 模型都充当查询构建器,因此请在一行中尝试以下操作:
Comment::where('post_id',$id)->delete();
Run Code Online (Sandbox Code Playgroud)
在 tinker 中测试,按预期工作,返回已删除行的计数。
文档:https : //laravel.com/docs/5.3/queries#deletes