How to count eloquent relationship without the N+1 issue and loading the entire model?

回眸只為那壹抹淺笑 提交于 2020-01-13 07:16:15

问题


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!