如何计算没有N + 1问题并加载整个模型的雄辩关系?

时间:2014-09-23 19:59:44

标签: php laravel laravel-4 eloquent eager-loading

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

我的CategoriesController索引功能:

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

Category模型与文章有很多关系:

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

编辑:如果我急于加载所有相关文章,它会有效,但由于我只需要文章计数pr类别,这似乎有点过分。是否会急切加载文章并->count()影响性能?或者这是最好的方法吗?

2 个答案:

答案 0 :(得分: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;
}

然后你可以这样做:

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

答案 1 :(得分:0)

试试这个

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

现在在视图中获取类别数据

$category->type或者如果您有姓名字段,则可以$category->name

获取名称

要获取您可以执行的类别的文章

foreach($category->articles as $article)
// do something with the articles
@endforeach

要获得某个类别的文章计数$category->articles->count();

确保您还阅读过Eager loading的文档,这确实有很大帮助。