2016-12-15 174 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