title: Laravel - group posts by months (eager loading)
body: <p>I would like to list my posts grouped by months</p>
  
  <p>This is my Category Model</p>
  
  <pre><code>class Category extends \Eloquent {
      public function posts()
      {
          return $this-&gt;belongsToMany('Post', 'category_post_tag')
                      -&gt;select(DB::raw('MONTH(posts.eventStartDate) month, MONTHNAME(posts.created_at) month_name'))
                      -&gt;distinct()
                      -&gt;orderBy('eventStartDate', 'asc');
      }
  }
  </code></pre>
  
  <p>So when i do this in my view file:</p>
  
  <pre><code>@foreach($category-&gt;posts as $index =&gt; $post)
      &lt;pre&gt;{{ $post }}&lt;/pre&gt;
  @endforeach
  </code></pre>
  
  <p>I get this result:</p>
  
  <pre><code>{"month":"7","month_name":"July","id":"20","slug":"sunt-et-ipsum-dolorum","title":"Sint dolorum exercitationem."}
  {"month":"7","month_name":"July","id":"15","slug":"qui-exercitationem-autem","title":"Id tenetur rem cumque ut."}
  {"month":"8","month_name":"July","id":"6","slug":"aspernatur-qui-repellat","title":"Recusandae accusamus omnis dicta."}
  {"month":"9","month_name":"July","id":"25","slug":"et-aut-eos-quaerat-soluta-perspiciatis","title":"Facere ullam sint et."}
  {"month":"10","month_name":"July","id":"26","slug":"sunt-nemo-aperiam","title":"Voluptatibus harum sunt et ducimus."}
  </code></pre>
  
  <p>Which is ok, because i only have 5 posts, but i would like somehow to group the posts which are in the same month so i can list them out in my view grouped by months</p>
  
  <p>I tried to add groupBy('month') to my posts method (relation) in my Category model but that only makes things worse, because then i only get back one post / month</p>
  
  <p>So i tried this in my view file:</p>
  
  <pre><code>@foreach($category-&gt;posts as $index =&gt; $post)
      {{ $new_array[$post-&gt;month][$post-&gt;id] = $post }}
  @endforeach
  </code></pre>
  
  <p>to create a new array grouped by the months, but this freezes my browser, it just keeps loading and in the end dies with a white screen.</p>
  
  <p>Could someone please point me in the right direction, thank you very much.</p>