title: Laravel left join only the most recent row
body: <p>I'm looking for a way (preferably using Laravel's query builder) to only join the "most recent row" on two tables that have a one-to-many relation.</p>
  
  <p>Here's a simplified example of what I mean:</p>
  
  <pre><code>Table 1 - robots
  robotID
  name
  
  Table 2 - locations
  locationID
  robotID
  latitude
  longitude
  date (Y-m-d)
  </code></pre>
  
  <p>I'd like to have a list of <code>robots</code> with only their most recent row from <code>locations</code> (ordered by <code>locations.date</code>).
  I've found that it sounds very simple (so the answer could be obvious!) but I can't get the expected outcome very easily without using a for-loop, which is very slow.</p>
  
  <p>The expected outcome is preferably each <code>robot</code> with their most recent <code>location</code>. I've tried the following, but it's not as expected. I could just be having a slow day.</p>
  
  <pre><code>Robot::whereIn('robots.robotID', [1,2,3,4])
  -&gt;leftJoin('locations', function ($join) {
      $join-&gt;on('locations.robotID', '=', 'robots.robotID');
  })
  -&gt;orderBy('locations.date', 'desc')
  -&gt;groupBy('robots.robotID')
  -&gt;get();
  </code></pre>
  
  <p>Thanks in advance!</p>