【问题标题】:How to show comments and replycomments in laravel如何在 laravel 中显示评论和回复评论
【发布时间】:2018-12-15 19:44:19
【问题描述】:

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

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

【问题讨论】:

    标签: laravel view laravel-blade


    【解决方案1】:

    内部刀片文件:

    <ul> @foreach($comments as $comment) <li>{{ $comment->comment_text }}</li> @endforeach </ul>

    【讨论】:

    • 我在表格中有cmets和回复评论,如何在评论底部显示每个回复cmets?
    • 我知道如何循环,然后在控制器内部定义这个变量。 $comments = Comment::with(['Sender', 'Replyer', 'Reply'])-&gt;get(); else die-dump 以上,你会明白的。访问发件人(循环内):$comment->sender->FIELDNAME
    【解决方案2】:

    控制器代码必须与此类似

    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>
    

    【讨论】:

    • 对不起,我的评论表有 id、user_id、post_id、reply_id、comment_text 字段。是显示评论和回复的控制器代码,Comment::with(['id','reply_id'])->get(); ?并在运行时显示模型错误。为什么?
    • 你在评论模型关系中定义了,能不能加个问题??
    • 不行,怎么办?
    • 添加CommentReplycomment模型
    猜你喜欢
    • 2013-06-05
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2022-11-18
    相关资源
    最近更新 更多