title: Laravel Eloquent group by most recent record
body: <p>I'm trying to get the most recent record for a single customer on a table. Example:</p>
  
  <pre><code>ID    Customer    City    Amount
  1     Cust001     City1   2
  2     Cust001     City2   3
  3     Cust001     City1   1
  4     Cust001     City2   1
  5     Cust001     City2   3
  6     Cust001     City3   1
  7     Cust001     City3   1
  8     Cust002     City1   2
  9     Cust002     City1   1
  10    Cust002     City2   3
  11    Cust002     City1   2
  12    Cust002     City2   1
  13    Cust002     City3   2
  14    Cust002     City3   3
  15    Cust003     City1   1
  16    Cust003     City2   3
  17    Cust003     City3   2
  </code></pre>
  
  <p>Please note that the table also has created_at and updated_at fields. I omitted those fields for simplicity.</p>
  
  <p>In the end I want my query to return for Cust001:</p>
  
  <pre><code>ID    Customer    City    Amount
  3     Cust001     City1   1
  5     Cust001     City2   3
  7     Cust001     City3   1
  </code></pre>
  
  <p>And for Cust002:</p>
  
  <pre><code>ID    Customer    City    Amount
  11    Cust002     City1   2
  12    Cust002     City2   1
  14    Cust002     City3   3
  </code></pre>
  
  <p>I've tried:</p>
  
  <pre><code>Table::where('Customer', 'Cust001')
      -&gt;latest()
      -&gt;groupBy('City')
      -&gt;get()
  </code></pre>
  
  <p>and also</p>
  
  <pre><code>Table::select(DB::raw('t.*'))-&gt;from(DB::raw('(select * from table where Customer = \'Cust001\' order by created_at DESC) t'))
      -&gt;groupBy('t.City')-&gt;get();
  </code></pre>
  
  <p>But it keeps returning the oldest record on each group (and I want the most recent).</p>
  
  <p>How can I achieve this? If is easier for you guys, you can write the SQL Query here and I will find a way to "translate it" to Laravel syntax.</p>