title: laravel use count/sum method after groupBy
body: <p>When I use <code>count</code> or <code>sum</code> method, I get the wrong result.</p>
  
  <p>For example:</p>
  
  <pre><code>Member::groupBy('id')-&gt;count()
  </code></pre>
  
  <p>I only get result 1, in another method</p>
  
  <pre><code>Member::count()
  </code></pre>
  
  <p>I get the right result.</p>
  
  <p>I get wrong result when use <code>sum</code> method.</p>
  
  <p><a href="https://github.com/laravel/framework/issues/14123" rel="nofollow noreferrer">https://github.com/laravel/framework/issues/14123</a></p>
  
  <p>Forgot to say, My laravel  version info:</p>
  
  <pre><code>Laravel Framework version 5.1.40 (LTS)
  </code></pre>
  
  <p><br><br>
  <strong>Actually problem in project</strong></p>
  
  <p>I have a  <strong>recharge log table</strong>.</p>
  
  <p><em>1.pay log table</em></p>
  
  <pre><code>CREATE TABLE pay_logs (
     id int(11) NOT NULL AUTO_INCREMENT,
     amount decimal(20,2) DEFAULT '0.00',
     pay_sn varchar(20) DEFAULT NULL,
     join_date int(11) unsigned DEFAULT '0',
     PRIMARY KEY (id)
  ) ENGINE=InnoDB DEFAULT CHARSET=utf8;
  </code></pre>
  
  <p><em>1.1 pay log talbe data</em></p>
  
  <pre><code>INSERT INTO pay_logs (id, amount, pay_sn, join_date)
  VALUES
    (1,10.00,'aabbcc',1466753291),
    (2,10.00,'aabbcc',1466753292),
    (3,20.00,'bbccdd',1466753292),
    (4,30.00,'ccddee',1466753292);
  </code></pre>
  
  <p>2.description</p>
  
  <p>In pay_log table, the 1 and 2 record is same. So when wants to filter the results by the actually recharge success time, I just need one record, so I use groupBy operation.</p>
  
  <p>My wrong operation</p>
  
  <pre><code>DB::table('pay_log')
   -&gt;whereBetween('joinDate',[$beginToday,$endToday])
   -&gt;where('isSucceed','=',1)
   -&gt;where('type','=',1)
   -&gt;groupBy('pay_sn');
   -&gt;count();
  </code></pre>
  
  <p>Finally,solve it. </p>
  
  <pre><code>$query = DB::table('pay_log')
   -&gt;select(DB::raw('count(*) as num'))
   -&gt;whereBetween('joinDate',[$beginToday,$endToday])
   -&gt;where('isSucceed','=',1)
   -&gt;where('type','=',1)
   -&gt;groupBy('pay_sn');
  </code></pre>
  
  <p>ps: if use Eloquent ORM</p>
  
  <p>modify <code>-&gt;mergeBindings($query)</code> to <code>-&gt;mergeBindings($query-&gt;getQuery())</code></p>
  
  <p><br><br>
  <strong>Test Example（<em>discard</em>）</strong></p>
  
  <ol>
  <li><p>create table sql</p>
  
  <p><code>CREATE TABLE members (
      id int(11) NOT NULL AUTO_INCREMENT,
      name varchar(15) DEFAULT NULL,
      amount decimal(20,2) DEFAULT '0.00',
      PRIMARY KEY (id)
    ) ENGINE=InnoDB AUTO_INCREMENT=4 DEFAULT CHARSET=utf8;</code></p></li>
  <li><p>test data</p>
  
  <p><code>INSERT INTO hc_members (id, name, amount) VALUES
      (1,'aaa',10.00),
      (2,'bbb',10.00),
      (3,'ccc',10.00);</code></p></li>
  <li><p>test result</p></li>
  </ol>
  
  <p>In my hope, when I use the method <code>Member::groupBy('name')-&gt;count()</code></p>
  
  <p>I want <strong>result 3</strong></p>
  
  <p>actually it <strong>returns 1</strong>.</p>
  
  <p><br>
  when I use the method <code>Member::groupBy('name')-&gt;sum('amount')</code></p>
  
  <p>I want <strong>result 30.00</strong></p>
  
  <p>actually it <strong>returns 10.00</strong></p>
  
  <p><br><br>
  <strong>Thanks for your answer to help me solve the problem!</strong></p>