我正在使用laravel PHP开发一个网站,并尝试使用以下结构进行评论系统:
- Comment 1 (id = 1)
-- Reply 1 (id = 2) (parent_id = 1)
--- Reply 2.1 (id = 3) (parent_id = 2)
-- Reply 2 (id = 4) (parent_id = 1)
我想知道我将如何做一个foreach来覆盖它?因为我不知道一条评论会有多少子评论。
我不会将评论和回复存储在单独的表中,因为它们在一天结束时都是评论实体。只需在comments
表中有一个parent_id
列,您就可以在一个数据库查询中获取注释和回复,而不是两个。
我假设你还有一个外键,将评论链接到类似帖子的东西。然后,您可以获取该帖子 ID 的所有评论:
$comments = Comment::latest()->where('post_id', '=', $post->id)->get();
然后根据它们的parent_id
值对它们进行排序:
$comments = $comments->keyBy('parent_id');
然后,可以在 Blade 模板中像这样迭代它们,每次迭代,检查是否有将该注释的 ID 作为其父 ID 的注释:
<!-- Kick-start the loop -->
@foreach($comments[0] as $comment)
@include('partials.comment')
@endforeach
部分/评论.刀片的内容.php
<blockquote class="comment">
<p class="comment-body">{{ $comment->body }}</p>
<footer>
<span class="comment-author">{{ $comment->user->name }}</span>,
<time class="comment-date" pubdate="pubdate">{{ $comment->created_at }}</time>
</footer>
</blockquote>
@if(isset($comments[$comment['id']])
@each('partials.comment', $comments[$comment['id'], 'comment')
@endif
Table Like:
+------------+-----------+---------+
| comment_id | parent_id | comment |
+------------+-----------+---------+
| 1 | 0 | test |
| 2 | 1 | test1 |
| 3 | 0 | test2 |
| 4 | 0 | test3 |
| 5 | 1 | test4 |
| 6 | 2 | test4 |
| 7 | 4 | test5 |
| 8 | 5 | test6 |
| 9 | 6 | test7 |
| 10 | 4 | test8 |
| 11 | 3 | test9 |
+------------+-----------+---------+
Get first level parent:
$comments = Comment::where('parent_id', '0')->orderBy('comment_id', 'asc')->get();
$result = array();
foreach($comments as $comment){
$list = array();
$list = array_merge($list, [['comment_id' => $comment->comment_id, 'parent_id' => $comment->parent_id, 'comment' => $comment->comment]]);
$result = array_merge($result, $this->get_child_comment($comment->comment_id,0, $list));
}
function get_child_comment($pid,$level,$list=array()) {
$sub_comments = Comment::where('parent_id','=',$pid)->where('comment_id','!=',$pid)->orderBy('comment_id', 'asc')->get();
foreach($sub_comments as $sub_comment){
$space=" "; sigm='-';
for($j=0; $j<=$level; $j++)
{
$space .=$space;
}
for($j=0; $j<=$level; $j++)
{
$space .= $sigm;
}
$sub_comment->comment = html_entity_decode($space, ENT_QUOTES, "utf-8").' '.$sub_comment->comment;
$list = array_merge($list, array(['comment_id' => $sub_comment->comment_id, 'parent_id' => $sub_comment->parent_id, 'comment' => $sub_comment->comment]));
$list = $this->get_child_comment($sub_comment->comment_id, $level+1, $list);
}
return $list;
}
}
return get array.simple print using foreach:
foreach($result as $val) {
echo $val['comment'].'<br>';
}
Output:
test
- test1
-- test4
--- test7
- test4
-- test6
test2
- test9
test3
- test5
- test8
您可以按以下结构表示注释:
$comments = [
(object)[
'id' => 1,
'text' => 'Comment 1',
'children' => [
(object)[
'id' => 2,
'text' => 'Reply 1',
'children' => [
(object)[
'id' => 2,
'text' => 'Reply 1.1'
]
]
],
(object)[
'id' => 4,
'text' => 'Reply 2',
]
]
]
];
并使用递归函数打印它们,如下所示:
function printComments($comments, $prefix = '-') {
foreach ($comments as $comment) {
echo $prefix.$comment->text.'<br>';
isset($comment->children) && printComments($comment->children, $prefix.'-');
}
}
或者在 Laravel 的情况下递归调用视图:
@include('comments.view.path') @include('comments.view.path')
为了方便地检索所表示结构中的注释以及通常使用树结构,我建议使用嵌套集模型和 etrepat/baum 对于具有 toHierarchy() 方法的 Laravel。