title: OrderBy in Laravel 5
body: <p>I’m trying to make a a basic twitter-eqs messaging app with Laravel 5.</p>
  <p>There are messages and senders. I’m trying to write a query which displays latest messages, the sender, and the total number of messages shown by the sender. The query should only show the latest message for each user. e.g. if one person has sent 6 messages in the last 5 mins, it’d only show the latest one from them.</p>
  <p>What I'm trying to do in an Eloquent way is:</p>
  <ol>
  <li>Query the latest messages, GroupBy Sender</li>
  <li>Select senders name, Join the Sender's name onto query 1</li>
  <li>Query count of messages GroupBy Sender, join onto query 2</li>
  </ol>
  <p>This process may be fundamentally wrong.</p>
  <p>Then, in my view, I'd render: Sender A - (31 messages), Hello this is my message.</p>
  <p><strong>TABLES</strong></p>
  <p>I have 2 tables</p>
  <p><strong>Senders</strong></p>
  <p>ID | Name</p>
  <p>and</p>
  <p><strong>Messages</strong></p>
  <p>ID | SenderID | Message | Created_at</p>
  <p><strong>QUERY</strong></p>
  <pre><code>$latestMessages = Sender::
  join('messages', 'messages.sender_id', '=', 'senders.id')
  -&gt;OrderBy('messages.created_at', 'asc')
  -&gt;GroupBy('senders.id')
  -&gt;get();
  </code></pre>
  <p><strong>PROBLEM</strong></p>
  <p>This outputs the OLDEST message, not the NEWEST. (also, I can't figure out how to count the messages from each sender and join this).</p>
  <p>I've tried changing the &quot;ASC&quot; and to &quot;DESC&quot; to no avail. When I remove the GroupBy, it <em>does</em> show the messages in the desired order (newest to oldest) but I can't work out how to only show 1 per sender.</p>
  <p><strong>QUESTION</strong>
  Could somebody explain how to achieve this with Laravel 5, or the floor in my approach and what I should look to do?</p>
  <p>If you can explain how query and join the count() in Eloquent, that'd be incredible.</p>