问题
I am showing a list of categories, and the article count within each category. I get the expected result, but I am having the N+1 problem.
My CategoriesController
index function:
public function index()
{
return View::make('categories.index', [
'articleCategories' => Category::where('type', 'articles')->orderBy('name')->get(),
]);
}
The Category
model has many relationship to articles:
public function articles()
{
return $this->hasMany('Article');
}
My categories.index
view:
@foreach($articleCategories as $articleCategory)
<p>
{{ HTML::link(URL::route('articles.category', array('category' => Str::slug($articleCategory->name))), $articleCategory->name) }}
{{ $articleCategory->articles->count() }}
</p>
@endforeach
Edit: It works if I eager load all related articles, but since I only need the article count pr category this seems overkill. Will eager loading articles and do ->count()
impact performance? Or is this the best way to do it?
回答1:
// helper relation
public function articlesCount()
{
return $this->hasOne('Article')->selectRaw('category_id, count(*) as aggregate')->groupBy('category_id');
}
// and accessor for fetching it easier
public function getArticlesCountAttribute()
{
if ( ! array_key_exists('articlesCount', $this->relations)) $this->load('articlesCount');
return $this->getRelation('articlesCount')->aggregate;
}
Then you can do this:
// eager load in single query
$categories = Category::with('articlesCount')->get();
// thanks to accessor this will return value and load relation only if needed
$categories->first()->articlesCount;
回答2:
Try this
public function index()
{
return View::make('categories.index', [
'category' => Category::with('articles')->where('type', 'articles')->orderBy('name')->get(),
]);
}
Now in View to get the category data just do
$category->type
or if you have a name field you can get the name by$category->name
To get the articles of the category you can do
foreach($category->articles as $article)
// do something with the articles
@endforeach
To get the count of articles of a category do $category->articles->count();
Make sure you also read documentation of Eager loading once, it really helps a lot.
来源:https://stackoverflow.com/questions/26003721/how-to-count-eloquent-relationship-without-the-n1-issue-and-loading-the-entire