title: Laravel - Eloquent - Return Where Related Count Is Greater Than
body: <p>I have 2 tables.</p>
  
  <p>Products
  Brands</p>
  
  <p>Im trying to return top 10 brand models with the most products.</p>
  
  <p>I've tried.</p>
  
  <pre><code>Product::select('brand', DB::raw('count(brand) as count'))-&gt;groupBy('brand')-&gt;orderBy('count','desc')-&gt;take(10)-&gt;get();
  </code></pre>
  
  <p>But that doesn't return the hole model and only returns</p>
  
  <ul>
  <li>Brand </li>
  <li>Count</li>
  </ul>
  
  <p>I've also tried</p>
  
  <pre><code> return $brands = Brand::whereHas('products', function($q) {
             $q-&gt;count() &gt; 10;
         })-&gt;get();
  </code></pre>
  
  <p>But I get the error:</p>
  
  <blockquote>
    <p>SQLSTATE[42S22]: Column not found: 1054 Unknown column 'brands.id' in
    'where clause' (SQL: select count(*) as aggregate from <code>products</code>
    where <code>brands</code>.<code>id</code> = <code>products</code>.<code>brand</code>)</p>
  </blockquote>
  
  <p><strong>My Brand Model</strong></p>
  
  <pre><code>public function products()
      {
          return $this-&gt;hasMany('App\Product','brand');
      }
  </code></pre>
  
  <p><strong>My Product Model</strong></p>
  
  <pre><code>public function manuf()
      {
          return $this-&gt;belongsTo('App\Brand','brand');
      }
  </code></pre>