title: Laravel imap get messages sorted by most recent
body: <p>I am using <a href="https://github.com/Webklex/laravel-imap" rel="nofollow noreferrer">laravel-imap</a> to retrieve messages from a mailbox:</p>
  
  <pre><code>$aMessage = $selectedFolder-&gt;query()-&gt;setFetchFlags(true)-&gt;setFetchBody(true)-&gt;setFetchAttachment(false)-&gt;leaveUnread()
  -&gt;limit(40, $highest_page)-&gt;get();
  </code></pre>
  
  <p>On default the most recent messages are on the highest page. If I simply get all messages and display them as they are I see the old ones first (page 1).</p>
  
  <p>What I tried so far:</p>
  
  <p>After I call <code>-&gt;get()</code> the results can be treated as a normal laravel collection, but I had no success with these methods:</p>
  
  <pre><code>$aMessage = $aMessage-&gt;sortByDesc('date')-&gt;values()-&gt;all();
  
  $aMessage = $aMessage-&gt;sortByDesc(function ($item) {
      //\Log::info(print_r($item, true));
      return $item-&gt;date;
  })-&gt;values()-&gt;all();
  </code></pre>
  
  <p>I am sure the methods above do not work because I use <code>-&gt;limit(40, $highest_page)</code>, which means the collection has a size of 40. </p>
  
  <p>If I dont limit the results and use slice I encounter a timeout error, because there are too many messages in the mailbox:</p>
  
  <pre><code>$aMessage = $selectedFolder-&gt;query()-&gt;setFetchFlags(true)-&gt;setFetchBody(true)-&gt;setFetchAttachment(false)-&gt;leaveUnread()
  -&gt;get()-&gt;reverse()-&gt;slice(0, $messages_per_page);
  </code></pre>
  
  <p>I also tried laravel-imap's method <code>paginate()</code>. The results were the same as with <code>limit()</code>.</p>
  
  <p>At this point I am sure I have to sort somehow before the results turn into a collection. I also found:</p>
  
  <pre><code>'options' =&gt; [
      'fetch_order' =&gt; 'desc',
  ]
  </code></pre>
  
  <p>The options are added to the Client. I tried asc and desc. I also cleared config cache, but nothing works. </p>
  
  <p>I still see the oldest results on page 1 and the most recent at the end.</p>
  
  <p>Someone has more ideas?</p>