laravel 5.1 以多对多关系获取每个类别的相关 5 条新闻
laravel 5.1 getting related 5 news of each category in many-to-many relation
我被困在这里已经尝试了 2-3 个小时。
我有一个多对多关系:
class Category extends Model
{
public function news()
{
return $this->belongsToMany('App\News');
}
}
class News extends Model
{
public function categories()
{
return $this->belongsToMany('App\Category');
}
}
我正在尝试获取相关类别的最新 5 条新闻:
$front_categories = Category::with(array(
'news'=>function($query){
$query->where('publish','1')->orderBy('created_at', 'desc')->take(5);}))
->where('in_front', 1)->get();
上面的查询对我不起作用,它给出了总共五个结果,而不是每个类别的 5 个结果。
我认为,因为您确实急切地加载了一个包含多个记录的集合。
求解需要循环
$front_categories = Category::where('in_front', 1)->get();
foreach ($front_categories as $fCategory) {
$fCategory->load(['news' => function($query) {
$query->where('publish','1')->orderBy('created_at', 'desc')->take(5);
}]);
}
此解决方案将对数据库执行许多查询。如果您只想处理 1 个查询,请查看此 Using LIMIT within GROUP BY to get N results per group?
根据我对Laravel的了解,您可以尝试这样做。
class Category {
public function recentNews()
{
return $this->news()->orderBy('created_by', 'DESC')
->take(5);
}
}
// Get your categories
$front_categories = Category::where('in_front', 1)->get();
// load the recent news for each category, this will be lazy loaded
// inside any loop that it's used in.
foreach ($front_categories as $category) {
$category->recentNews;
}
这与 Lê Trần Tiến Trung 的回答具有相同的效果,并导致多个查询。它还取决于您是否重用此功能。如果是一次性的,最好将其放在其他地方。其他方法也可以更动态,例如创建一个 returns 类别集合的方法,你可以向它询问某个数字:
class CategoriesRepository {
public static function getFrontCategories(array $opts = []) {
$categories = Category::where('in_front', 1)->get();
if (!empty($opts) && isset($opts['withNewsCount']))
{
foreach ($categories as $category)
{
$category->recentNews = static::getRecentNewsForCategory(
$category->id,
$opts['withNewsCount']
);
}
}
return $categories;
}
}
$front_categories = CategoriesRepository::getFrontCategories([
'withNewsCount' => 5
]);
我被困在这里已经尝试了 2-3 个小时。
我有一个多对多关系:
class Category extends Model
{
public function news()
{
return $this->belongsToMany('App\News');
}
}
class News extends Model
{
public function categories()
{
return $this->belongsToMany('App\Category');
}
}
我正在尝试获取相关类别的最新 5 条新闻:
$front_categories = Category::with(array(
'news'=>function($query){
$query->where('publish','1')->orderBy('created_at', 'desc')->take(5);}))
->where('in_front', 1)->get();
上面的查询对我不起作用,它给出了总共五个结果,而不是每个类别的 5 个结果。
我认为,因为您确实急切地加载了一个包含多个记录的集合。
求解需要循环
$front_categories = Category::where('in_front', 1)->get();
foreach ($front_categories as $fCategory) {
$fCategory->load(['news' => function($query) {
$query->where('publish','1')->orderBy('created_at', 'desc')->take(5);
}]);
}
此解决方案将对数据库执行许多查询。如果您只想处理 1 个查询,请查看此 Using LIMIT within GROUP BY to get N results per group?
根据我对Laravel的了解,您可以尝试这样做。
class Category {
public function recentNews()
{
return $this->news()->orderBy('created_by', 'DESC')
->take(5);
}
}
// Get your categories
$front_categories = Category::where('in_front', 1)->get();
// load the recent news for each category, this will be lazy loaded
// inside any loop that it's used in.
foreach ($front_categories as $category) {
$category->recentNews;
}
这与 Lê Trần Tiến Trung 的回答具有相同的效果,并导致多个查询。它还取决于您是否重用此功能。如果是一次性的,最好将其放在其他地方。其他方法也可以更动态,例如创建一个 returns 类别集合的方法,你可以向它询问某个数字:
class CategoriesRepository {
public static function getFrontCategories(array $opts = []) {
$categories = Category::where('in_front', 1)->get();
if (!empty($opts) && isset($opts['withNewsCount']))
{
foreach ($categories as $category)
{
$category->recentNews = static::getRecentNewsForCategory(
$category->id,
$opts['withNewsCount']
);
}
}
return $categories;
}
}
$front_categories = CategoriesRepository::getFrontCategories([
'withNewsCount' => 5
]);