如何计算雄辩的关系,没有N + 1问题并加载整个模型? [英] How to count eloquent relationship without the N+1 issue and loading the entire model?

查看:88
本文介绍了如何计算雄辩的关系,没有N + 1问题并加载整个模型?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在显示一个类别列表,以及每个类别中的文章计数。我得到预期的结果,但是我遇到了N + 1个问题。

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.

我的 CategoriesController index function: / p>

My CategoriesController index function:

public function index()
{
    return View::make('categories.index', [
        'articleCategories' => Category::where('type', 'articles')->orderBy('name')->get(),
    ]);
}

类别与文章有很多关系:

public function articles()
{
    return $this->hasMany('Article');
}

我的 categories.index 查看:

@foreach($articleCategories as $articleCategory)
    <p>
    {{ HTML::link(URL::route('articles.category', array('category' => Str::slug($articleCategory->name))), $articleCategory->name) }}
    {{ $articleCategory->articles->count() }}
    </p>
@endforeach

编辑:如果我渴望加载所有相关的文章,但由于我只需要文章计数类别这似乎是过度的。会急于加载文章,并执行 - > count()影响性能?或者这是最好的方式吗?

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?

推荐答案

// 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;
}

然后你可以这样做:

// 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;

这篇关于如何计算雄辩的关系,没有N + 1问题并加载整个模型?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆