title: Laravel Query Builder - Sum Where and Sum Where
body: <p>I have a table called stock_movements:</p>
  
  <pre><code>| product_id | type | qty  |
  |------------|------|------|
  | 1          | A    | 2    |
  | 1          | A    | 1    |
  | 1          | A    | 7    |
  | 1          | B    | -2   |
  | 1          | B    | -4   |
  | 1          | B    | -1   |
  | 2          | A    | 2    | 
  | 2          | A    | 1    |
  | 2          | A    | 7    | 
  | 2          | B    | -3   | 
  | 2          | B    | -3   |
  | 2          | B    | -1   |
  </code></pre>
  
  <p>I am trying to create a collection of the products where which have the values of the sum of A and sum of B.</p>
  
  <p>i.e. 
  Group by products, type
  Sum qty for each type</p>
  
  <p>The key thing is that it is a collection against the product, which is the bit I am struggling with. Does anyone have any advice?</p>
  
  <p>So far I have got...</p>
  
  <pre><code>    return $this-&gt;stockMovements()-&gt;groupBy('product_id')
          -&gt;selectRaw('sum(qty), product_id')
          -&gt;where('type', $this-&gt;type)
          -&gt;get();
  </code></pre>
  
  <p>But this does not split out for types A and B.</p>