I use Laravel 5.3.
I have 2 tables :
Articles --------- id cat_id title
And
Category --------- id parent_id title
Define a articles() relation in your Category model as:
articles()
Category
public function articles() { return $this->hasMany(Article::class, 'cat_id'); }
Then you can try it as:
Category::where('parent_id', 0)->withCount('articles')->get();