Display comments to the page

2 answers

I have a problem posting comments on the page depending on the posting id.

Controller:

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

Route:

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

View:

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

What do you think will be the problem? Do you think I'm making the wrong display or?

Received answers to this question:
The best answer according to the author of the question:

at this side

change

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

to

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

and change the view to

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

hope this help :)

Change your code like below it will work perfectly.

Controller:

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

View:

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