title: Laravel eloquent get relation count
body: <p>I use Laravel 5.3. </p>
  
  <p>I have 2 tables :</p>
  
  <pre><code>Articles
  ---------
  id
  cat_id
  title
  </code></pre>
  
  <p>And</p>
  
  <pre><code>Category
  ---------
  id
  parent_id
  title
  </code></pre>
  
  <p>I have defined my relations in my models : </p>
  
  <pre><code>// Article model
  public function category()
  {
      return $this-&gt;belongsTo(Category::class);
  }
  
  // Category model
  public function children() 
  {
      return $this-&gt;hasMany(Category::class, 'parent_id', 'id');
  }   
  </code></pre>
  
  <p>Is there an easy way using Eloquent to have a list a categories with count of articles. The difficulty is that I want to group categories where <code>id_parent = 0</code>, i.e. I want to display only parent categories with count of articles in children.</p>
  
  <p>I tried something like that :</p>
  
  <pre><code>    $category = new \App\Models\Category();
      $categoryTable = $category-&gt;getTable();
  
      return $category-&gt;leftJoin('article', 'article.cat_id', '=', 'category.id')
          -&gt;whereIn('article.cat_id', function($query)
              {
                  $query-&gt;select('cat_id')
                      -&gt;from('categories')
                      -&gt;where('categories.parent_id', ???)
                      -&gt;orWhere($this-&gt;tableName .'.cat_id', $id);
              })
          -&gt;groupBy('cat_id');
  </code></pre>
  
  <p>But I am lost...</p>