首页 > 解决方案 > 如何在 laravel 中显示评论和回复评论

问题描述

我不知道如何在帖子页面中显示评论和回复评论,我的评论表包含 id、sender_id、replyer_id、reply_id、comment_text。

CommentController 返回评论对象。我的问题是在 Post.blade.php 如何编写 foreach 循环或循环。请帮我。

标签: laravelviewlaravel-blade

解决方案


控制器代码必须与此类似

public function index() 
{
    $comments = Comment::with(['sender', 'other-relation'])->get();
    return view('comments.index', compact('comments'));
}

在刀片代码中必须与此类似

<ul>
@foreach($comments as $comment)
    <li>{{ $comment->comment_text }}</li>
    @if ($comment->sender) // or other relation 
        <a> {{$comment->sender->name}}<a> // relation name and column name must be fix yourself
    @endif
@endforeach
</ul>

推荐阅读