【发布时间】:2013-11-26 18:34:22
【问题描述】:
表格
post category
----- ----------
id id
name name
category_id
每个类别的帖子数量有限(如果类别有帖子)
$categories = Category::get();
$categores_with_posts = array();
foreach($categories as $category)
{
$category_data = array(
'posts' => Post::where('category_id', $category['id'])->take(10)->get()->toArray(),
'category_id' => $category['id'],
'name' => $category['name']
);
if(!empty($category_data['posts'])) {
$categores_with_posts[] = $category_data;
}
}
如何在 Laravel 中使用一个查询来做到这一点?
【问题讨论】:
标签: php mysql laravel laravel-4