title: Laravel 5 - multi-dimensional groupBy, orderBy & paginate
body: <p>I have a Post model and I am trying to <code>-&gt;paginate()</code>, <code>-&gt;groupBy()</code> and <code>-&gt;orderBy()</code>.</p>
  <pre><code>public function index()
  {
      $posts = Post::where('verified', '1')
              -&gt;orderBy('created_at','desc')
              -&gt;groupBy('topic', 'publisher_id')
              -&gt;paginate(5);
  
      // dd($posts);
  }
  </code></pre>
  <p>Meanwhile, data in Database looks like this:</p>
  <pre><code>|  id  |  verified  |  topic  | publisher_id |  body  | created_at |
  |  25  |      1     |  Forest |       3      |   EE   |  10.12.50  |
  |  24  |      1     |  Forest |       3      |   DD   |  10.11.40  |
  |  23  |      1     |  Forest |       3      |   CC   |  10.10.30  |
  
  |  22  |      1     |  Dance  |       2      |   BB   |   9.50.50  |
  |  21  |      1     |  Dance  |       2      |   AA   |   9.40.40  |
  
  |  20  |      1     |  Music  |       1      |   ZZ   |   9.30.30  |
  |  19  |      1     |  Music  |       1      |   XX   |   9.20.20  |
  |  18  |      1     |   Art   |       1      |   YY   |   9.10.10  |
  |  17  |      1     |   Art   |       1      |   WW   |   9.00.00  |
  
  |  16  |      1     |   Ski   |       2      |   KK   |   7.00.00  |
  </code></pre>
  <p>When I uncomment the the <code>dd()</code> and run the code, I get this log:</p>
  <pre><code> LengthAwarePaginator {
                         ...
                        items : {
                             items : {
                                     0 =&gt; Post{..}
                                     1 =&gt; Post{..}
                                     2 =&gt; Post{..}
                                     3 =&gt; Post{..}
                                       ...
                                     }
                                }
                          ...
                         }
  </code></pre>
  <blockquote>
  <p><code>0 =&gt; Post{#249} : &quot;published_by: &quot;3&quot;, &quot;body&quot;: &quot;CC&quot;, &quot;created_at&quot;: &quot;10.10.30&quot;</code></p>
  <p><code>1 =&gt; Post{#250} : &quot;published_by: &quot;1&quot;, &quot;body&quot;: &quot;XX&quot;, &quot;created_at&quot;: &quot;9.20.20&quot;</code></p>
  <p><code>2 =&gt; Post{#251} : &quot;published_by: &quot;1&quot;, &quot;body&quot;: &quot;WW&quot;, &quot;created_at&quot;: &quot;9.00.00&quot;</code></p>
  <p><code>3 =&gt; Post{#252} : &quot;published_by: &quot;2&quot;, &quot;body&quot;: &quot;KK&quot;, &quot;created_at&quot;: &quot;7.00.00&quot;</code></p>
  </blockquote>
  <p>It looks so weird for a reason. It did <code>groupBy</code> for user-3, but not for others. Also, it's pulling the earliest created at rather than latest. Changing <code>desc</code> to <code>asc</code> like <code>-&gt;orderBy('created_at', 'asc')</code>would put everything completely out of track.</p>
  <p>In other words, <strong>returning <code>'CC' for user-3, Forest</code> instead of <code>'EE' for user-3, Forest</code></strong></p>
  <p>Then I thought maybe it's the <code>-&gt;paginate(5)</code> messing things up.</p>
  <pre><code>public function post()
  {
      $posts = Post::where...
                          ...
              -&gt;paginate(5);
  
      $postsTry = Post::where('verified', '1')
              -&gt;orderBy('created_at','desc')
              -&gt;groupBy('topic', 'publisher_id')
              -&gt;get();
      
       // dd($postsTry);
   }
  </code></pre>
  <p>I get a Collection with only the <code>items</code> like above object in it.
  (<code>0 =&gt; Post{..}, 1 =&gt; Post{..}, 2 =&gt; Post{..}</code>).</p>
  <p><strong>It's grouping data as being <em>earliest first</em> rather than <em>latest first</em>.</strong> What I am missing? What is the thing I am doing wrong?</p>
  <hr />
  <blockquote>
  <p>To wrap up, <strong>note that what I want to get is:</strong></p>
  <p><code>'EE' for user-3, Forest</code></p>
  <p><code>'BB' for user-2, Dance</code></p>
  <p><code>'ZZ' for user-1, Music</code></p>
  <p><code>'YY' for user-1, Art</code></p>
  </blockquote>