title: Eloquent count distinct returns wrong totals
body: <p>i'm having an issue with how eloquent is formulation a query that i have no access to. When doing something like </p>
  
  <pre><code>$model-&gt;where('something')
  -&gt;distinct()
  -&gt;paginate();
  </code></pre>
  
  <p>eloquent runs a query to get the total count, and the query looks something like</p>
  
  <pre><code>select count(*) as aggregate from .....
  </code></pre>
  
  <p>The problem is that if you use distinct in the query, you want something like </p>
  
  <pre><code>select count(distinct id) as aggregate from .....
  </code></pre>
  
  <p>to get the correct total. Eloquent is not doing that though, thus returning wrong totals. The only way to get the distinct in count is to pass an argument through the query builder like so ->count('id') in which case it will add it. Problem is that this query is auto-generated and i have no control over it.</p>
  
  <p>Is there a way to trick it into adding the distinct on the count query? </p>
  
  <p>P.S Digging deep into the builders code we find an IF statement asking for a field on the count() method in order to add the distinct property to the count. Illuminate\Database\Query\Grammars\BaseGrammar@compileAggregate</p>
  
  <pre><code>if ($query-&gt;distinct &amp;&amp; $column !== '*')
  {
     $column = 'distinct '.$column;
  }
  return 'select '.$aggregate['function'].'('.$column.') as     aggregate';
  </code></pre>
  
  <p>P.S.1 I know that in SQL you could do a group by, but since i'm eager loading stuff it is not a good idea cause it will add a IN (number of id's found) to each of the other queries which slows things down significantly.</p>