使用Laravel的PHP评论系统

时间:2016-01-26 11:05:19

标签: php laravel for-loop

我正在使用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来覆盖它?因为我不知道评论会有多少个孩子评论。

3 个答案:

答案 0 :(得分:5)

不会将评论和回复存储在单独的表格中,因为它们在一天结束时都是评论实体。只需在parent_id表中添加comments列,就可以在一个数据库查询中获取注释和回复,而不是两个。

我假设您还有一个外键将评论链接到帖子之类的内容。然后,您可以获取该帖子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

partials / comment.blade.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

答案 1 :(得分:1)

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="&nbsp;"; 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

答案 2 :(得分:0)

您可以在以下结构中表示评论:

$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')

为了方便地检索所表示结构中的注释,并且通常使用树结构,我建议使用nested set modeletrepat/baum用于具有 toHierarchy()方法的Laravel。< / p>