title: Laravel ordering results of a left join
body: <p>I am trying to replicate the below SQL in Laravels Eloquent query builder.</p>
  
  <pre><code>select a.name, b.note from projects a 
  left join (select note, project_id from projectnotes order by created_at desc) as b on     (b.project_id = a.id) 
  where projectphase_id = 10 group by a.id;
  </code></pre>
  
  <p>So far I have:</p>
  
  <pre><code>$projects = Project::leftjoin('projectnotes', function($join)
  {
    $join-&gt;on('projectnotes.project_id', '=', 'projects.id');
  })
  -&gt;where('projectphase_id', '=', '10')
  -&gt;select(array('projects.*', 'projectnotes.note as note'))
  -&gt;groupBy('projects.id')
  -&gt;get()
  </code></pre>
  
  <p>which works for everything except getting the most recent projectnotes, it just returns the first one entered into the projectnotes table for each project. </p>
  
  <p>I need to get the order by 'created_at' desc into the left join but I don't know how to achieve this.</p>
  
  <p>Any help would be much appreciated. </p>