【发布时间】:2017-07-29 19:24:56
【问题描述】:
我为具有许多“cmets”的“公告”设置了一对多关系。
目前,当我的用户加载应用页面时,我让它发送 30 个最新的公告,如下所示:
Route::get('/app', function () {
$posts = Announcement::take(30)->orderBy('id', 'desc')->get();
return View::make('app')->with([
//posts
'posts' => $posts,
//orders
'orders' => $orders
]);
}
当我通过 $posts 对象使用 foreach 循环在刀片中回显公告时,我还想在相应帖子下回显每个帖子的 cmets。
是否可以将帖子的 cmets 作为实际帖子对象的一部分传递?例如,如果我能这样做就好了:
@foreach ($posts as $post)
//echo out the post
{{$post->content}}
//echo out the comments relating to this post
{{$post->comments}}
@endforeach
【问题讨论】:
标签: php html laravel orm eloquent