我使用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');
但我输了...
如何层次的多层次将在那里表中的'''category'''? – Dev
只有2个最大值,不是更多 –