我有一個一對多的關係,爲我的「公告」設置了許多「評論」。Laravel Pass對帖子的評論
目前,當我的用戶負荷高達應用程序頁面,我把它送30個最近宣佈像這樣:
Route::get('/app', function() {
$posts = Announcement::take(30)->orderBy('id', 'desc')->get();
return View::make('app')->with([
//posts
'posts' => $posts,
//orders
'orders' => $orders
]);
}
當我回聲出使用foreach循環通過$帖子在葉片上的公告對象,我還想在各自的帖子中回覆每篇文章的評論。
是否可以將帖子的評論作爲實際帖子對象的一部分傳遞給該帖子?例如,這將是很好,如果我能做到這一點:
@foreach ($posts as $post)
//echo out the post
{{$post->content}}
//echo out the comments relating to this post
{{$post->comments}}
@endforeach