0
試圖找到從數據庫中顯示帖子及其所有評論的最佳方式。我知道必須有更好的方式,但我不能拿出一個。顯示帖子評論的最有效方式
在我的控制,我發送到視圖:
$posts = Post::all();
$comments = Comment::all();
return view('layouts.main',compact('posts,comments'));
在我看來,我顯示每個崗位與所有的低於其意見。
@foreach($posts as $post)
{{ $post->content }}
@foreach($comments as $comment)
@if($comment->post->id == $post->id)
{{ $comment->content }}
@endif
@endforeach
@endforeach