我有这样的关系:如何统计有多少物品属于哪个类别?
文章
public function category()
{
return $this->belongsTo('App\Models\Categories');
}
类别有翻译
public function c_translations()
{
return $this->hasMany('App\Models\CategoryTranslations', 'category_id');
}
在文章中,我有类别ID,同样在翻译我已经CATEGORY_ID。那么我怎样才能算出每个类别有多少篇文章。任何建议?
$articles = Articles::all();
foreach($articles as $article){
$articles_category = Articles::where('id',$article->id)->withCount('category')->first();
}
我试过,但总能得到0所有类别
你的问题不清楚。你能举一些例子吗? –
我编辑了我的问题 – uzhas
你的问题仍然没有任何意义。你能举一些你需要的例子吗? –