【发布时间】:2017-04-15 16:53:48
【问题描述】:
我有一个包含以下列的 cmets 表
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();
这是获取每个答案的父 cmets 的查询
我试图这样获取子和子子 cmets
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>';
}
}
有人建议制作另一张桌子,里面有子和子cmets,但我想在一张桌子上做所有事情,请有人帮忙
【问题讨论】:
标签: php mysql laravel-5 laravel-5.3