Trying to find the best way to display posts from a database with all of their comments. I know there has to be a better way but I can't come up with one.
In my controller, I am sending to a view the following:
$posts = Post::all();
$comments = Comment::all();
return view('layouts.main',compact('posts,comments'));
In my view I am displaying each post with all of its comments below.
@foreach($posts as $post)
{{ $post->content }}
@foreach($comments as $comment)
@if($comment->post->id == $post->id)
{{ $comment->content }}
@endif
@endforeach
@endforeach
via Chebli Mohamed
Aucun commentaire:
Enregistrer un commentaire