我有這樣的關係:如何統計有多少物品屬於哪個類別?
文章
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
你的問題仍然沒有任何意義。你能舉一些你需要的例子嗎? –