我有一个注释表,其中包含以下列
id,answer_id,user_id,parent_id
我试图建立一个嵌套的注释系统,用户可以回复父注释,也可以回复子注释。
我只想做两个层次(不是完全嵌套的)
$comments = DB::table('comments')
->join('answers', 'answers.id' , '=', 'comments.answer_id')
->join('users' , 'users.id' , '=', 'comments.user_id')
->where('answers.id', '=' , $answer_id)
->where('parent_id', '0')
->select('comments.comment as comment',
'comments.id as comment_id',
'comments.created_at as created_at',
'comments.parent_id as parent_id',
'users.first_name as first_name',
'users.last_name as last_name',
// 'answers.aanswer as answer',
'answers.id as ans_id')
->orderBy('created_at', 'desc')
->get();这是一个查询,它获取每个答案的父批注。
我试着这样做来获取孩子和子孩子的评论
foreach ($comments as $comment) {
echo $comment->comment_id.$comment->comment.'<br>';
child_comment($comment->comment_id);
}这是我在foreach循环中调用的函数,用于追加子注释和子注释,但没有帮助。
public function child_comment($pid){
$child_comments = Comment::where('id', $pid)
->where('parent_id', '!=','0')
// ->orderBy('created_at', 'desc')
->select('comment')
->get();
foreach($child_comments as $child_comment){
echo $child_comment->comment.'<br>';
}
}有人建议再做一张表,上面会有子表和子表,但是我只想在一张桌子上做所有的事情,请有人帮忙。
发布于 2017-04-16 11:01:52
首先,我建议使用雄辩(它将完成大部分工作本身)。
如果不测试您的解决方案,我认为问题在于:
child_comment($comment->parent_id);
如果正确的话,您应该将注释ID而不是parent_id传递到这个函数中,以获得子注释。
当传入parent_id时,查询将始终不返回任何内容,因为您检查注释parent_id是否为0。“第一级”评论永远不会有parent_id。
https://stackoverflow.com/questions/43428708
复制相似问题