2016-12-15 57 views
3

我使用Laravel 5.3。Laravel雄辩得到关系计数

我有2个表:

Articles 
--------- 
id 
cat_id 
title 

而且

Category 
--------- 
id 
parent_id 
title 

我已经在我的模型中定义我的关系:

// Article model 
public function category() 
{ 
    return $this->belongsTo(Category::class); 
} 

// Category model 
public function children() 
{ 
    return $this->hasMany(Category::class, 'parent_id', 'id'); 
} 

有没有用雄辩有一个简单的方法列出文章数量的类别。难点在于我想对id_parent = 0的类别进行分组,即我只想显示父类别与儿童文章的数量。

我想类似的东西:

$category = new \App\Models\Category(); 
    $categoryTable = $category->getTable(); 

    return $category->leftJoin('article', 'article.cat_id', '=', 'category.id') 
     ->whereIn('article.cat_id', function($query) 
      { 
       $query->select('cat_id') 
        ->from('categories') 
        ->where('categories.parent_id', ???) 
        ->orWhere($this->tableName .'.cat_id', $id); 
      }) 
     ->groupBy('cat_id'); 

但我输了...

+0

如何层次的多层次将在那里表中的'''category'''? – Dev

+0

只有2个最大值,不是更多 –

回答

2

Category模型定义articles()关系为:

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

那么你可以试试as:

Category::where('parent_id', 0)->withCount('articles')->get(); 
+0

但是,如何计算带孩子的父类别中的文章数? –

+0

请检查更新的答案。 –

+0

是的,但我也需要儿童类别的数量。我的意思是如果一篇文章属于一个儿童类别,我需要把它计算在内。 –

4

您可以使用withCount()。它可以从5.3版本

约雄辩更多信息请访问:https://laravel.com/docs/5.3/eloquent-relationships

+0

好啊!它有效,但我在这里有2个关系。它适用于关系文章 - >类别,但我也需要包括孩子。 –

0

这应该工作:

$category 
->where('categories.parent_id', 0) 
->leftJoin('article', 'article.cat_id', '=', 'categories.id') 
->select('categories.id', \DB::raw('COUNT(article.id)')) 
->groupBy('categories.id') 
->get(); 

上面的查询将让你属于类别ID的所有物品计数类别。

在再次阅读您的问题和意见后,如果我理解正确,您希望获得属于这些类别的所有文章的计数(带有parent_id = 0)+属于子类别的文章的计数(带有parent_id的文章的计数=(某个类别的ID))。

现在我无法轻松测试这个,但我认为沿着这些线应该为此工作。

$category 
->where('categories.parent_id', 0) 
->leftJoin('article', 'article.cat_id', '=', 'categories.id') 
->leftJoin('categories as c2', 'c2.parent_id', '=', 'categories.id') 
->leftJoin('article as a2', 'a2.cat_id', '=', 'c2.id') 
->select('categories.id', \DB::raw('(COUNT(article.id)) + (COUNT(a2.id)) as count')) 
->groupBy('categories.id') 
->get(); 

也就是说beign说,我觉得你有在类名为列数和每一个新的文章被添加时更新的更好。表现。

2

您可以使用 Eloquent方法来获取所有儿童的文章,然后将文章数加在一个很好的小吸气器中。我将吸气剂添加到模型上的$appends阵列上,以帮助在修补器输出中对其进行说明。

class Category extends Model 
{ 

    protected $appends = [ 
     'articleCount' 
    ]; 

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

    public function children() 
    { 
     return $this->hasMany(Category::class, 'parent_id'); 
    } 

    public function childrenArticles() 
    { 
     return $this->hasManyThrough(Article::class, Category::class, 'parent_id'); 
    } 

    public function getArticleCountAttribute() 
    { 
     return $this->articles()->count() + $this->childrenArticles()->count(); 
    } 
} 

这里的廷克输出:

Psy Shell v0.8.0 (PHP 7.0.6 — cli) by Justin Hileman 
>>> $cat = App\Category::first(); 
=> App\Category {#677 
    id: "1", 
    name: "Cooking", 
    parent_id: null, 
    created_at: "2016-12-15 18:31:57", 
    updated_at: "2016-12-15 18:31:57", 
    } 
>>> $cat->toArray(); 
=> [ 
    "id" => 1, 
    "name" => "Cooking", 
    "parent_id" => null, 
    "created_at" => "2016-12-15 18:31:57", 
    "updated_at" => "2016-12-15 18:31:57", 
    "articleCount" => 79, 
    ] 
>>> 

如果你想限制你的类别查询到那些有有篇儿,你能做到这一点使用has()方法:

Category::has('children.articles')->get(); 

更多关于has()方法:

https://laravel.com/docs/5.3/eloquent-relationships#querying-relationship-existence

而且hasManyThrough()方法:

https://laravel.com/docs/5.3/eloquent-relationships#has-many-through