title: Calculating value differences between two records in Eloquent
body: <p>I would like to calculate the difference between two records in eloquent. For example, I have following table:</p>
  
  <pre><code>----------------------------------
  | Id | value | type              |
  ----------------------------------
  | 1  | 100   | FOO               |
  | 2  | 500   | FOO               |
  | 3  | 800   | FOO               |
  | 4  | 200   | BAR               |
  | 5  | 600   | BAR               |
  | 6  | 1000  | FOO               |
  ----------------------------------
  </code></pre>
  
  <p>Let's say the model name is FooBar, so when I filter the table, for example, using <code>FooBar::where('type', 'FOO')-&gt;get();</code> I will get following result:</p>
  
  <pre><code>----------------------------------
  | Id | value | type  | diff      |
  ----------------------------------
  | 1  | 100   | FOO   | 0         |
  | 2  | 500   | FOO   | 400       | (500 - 100)
  | 3  | 800   | FOO   | 300       | (800 - 500)
  | 6  | 1000  | FOO   | 200       | (1000 - 800)
  ----------------------------------
  </code></pre>
  
  <p>Now, probably I could achieve this more easily with raw query, like declaring variable to store previous record (e.g: SET @id := 0 and set it in SELECT statement). But in this case I prefer to use Eloquent if possible.</p>
  
  <p>My current workaround is loop the result set and calculate manually which I'm afraid it will affect the performance.</p>
  
  <p>Any ideas?</p>