title: Laravel Group By relationship column
body: <p>I have <code>Invoice_Detail</code> model which handles all products and it's quantities, this model table <code>invoice_details</code> has <code>item_id</code> and <code>qty</code> columns.</p>
  
  <p>The <code>Invoice_Detail</code> has a relation to <code>Items</code> model which holds all item's data there in its <code>items</code> table, which has <code>item_id</code>, <code>name</code>, <code>category_id</code>.</p>
  
  <p>The <code>Item</code> model also has a relation to <code>Category</code> model which has all categories data in its <code>categories</code> table.</p>
  
  <p>Question: I want to select top five categories from <code>Invoice_Detail</code>, how?</p>
  
  <p>Here's what I did:</p>
  
  <pre><code>$topCategories = InvoiceDetail::selectRaw('SUM(qty) as qty')
      -&gt;with(['item.category' =&gt; function($query){
          $query-&gt;groupBy('id');
      }])
      -&gt;orderBy('qty', 'DESC')
      -&gt;take(5)-&gt;get();
  </code></pre>
  
  <p>But didn't work !!</p>
  
  <pre><code>[{"qty":"11043","item":null}]
  </code></pre>