【问题标题】:Display comments to the page显示对页面的评论
【发布时间】:2020-07-28 06:16:43
【问题描述】:

我在页面上发布 cmets 时遇到问题,具体取决于发布 ID。

控制器:

 public function viewUserQuestion(Post $post) { 
    $comment = Comment::where('post_id', $post->id)->get();
    return view('viewQuestion', compact('post','comment'));
}

路线:

Route::get('/viewUserQuestion/{post}', 'PostsController@viewUserQuestion')->name('viewQuestion');

查看:

 @foreach($post->comments as $comment)
                        <span class="m-b-15 d-block" align="center">
                            {!! $comment->commentText !!}
                        </span> 
 @endforeach

您认为会出现什么问题?你认为我做错了展示还是?

【问题讨论】:

  • 我在页面上发布 cmets 时遇到问题 什么问题。我有问题对我们帮助你真的没有任何帮助。请告诉我们您有什么问题
  • 1.在控制器 2 中将 comment 重命名为 comments。在视图中将 $post-&gt;comments 更改为 $comments

标签: php html mysql laravel laravel-blade


【解决方案1】:

在这边

改变

return view('viewQuestion', compact('post','comment'));

return view('viewQuestion', ['comment'=>$comment]);

并将视图更改为

 @foreach($comment as $comment)
                    <span class="m-b-15 d-block" align="center">
                        {!! $comment->commentText !!}
                    </span> 
 @endforeach

希望得到帮助:)

【讨论】:

    【解决方案2】:

    像下面这样更改您的代码将完美运行。

    控制器:

    public function viewUserQuestion(Post $post) { 
        $comments = Comment::where('post_id', $post->id)->get();
        return view('viewQuestion', compact('comments'));
    }
    

    查看:

    @foreach($comments as $comment)
        <span class="m-b-15 d-block" align="center">
            {!! $comment->commentText !!}
        </span> 
     @endforeach
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2012-06-30
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-07-31
      • 1970-01-01
      • 2012-07-14
      • 1970-01-01
      相关资源
      最近更新 更多