title: Laravel 5 form another array from database result
body: <p>I'm new to Laravel and I've been struggling too long with this now, tried to search SO and google for information but can't solve this.</p>
  
  <p>I'm creating blog and need to make some kind of navigation/archive which displays year and months and how many blog posts there have been per year and invidual month. By clicking those years/months I would then display posts during that time period on different view.</p>
  
  <p>I want them to be displayed in the view like this:</p>
  
  <pre><code>2015 (10)
    January(3)
    February(3)
    March(3)
    April(1)
  2014 (2)
    May(1)
    June(1)
  </code></pre>
  
  <p>And so on.</p>
  
  <p>I got database query like this:</p>
  
  <pre><code>    $links = \DB::table('posts')
      -&gt;select(\DB::raw('YEAR(created_at) year, MONTH(created_at) month, MONTHNAME(created_at) month_name, COUNT(*) id'))
      -&gt;where('active',1)
      -&gt;groupBy('year')
      -&gt;groupBy('month')
      -&gt;orderBy('year', 'desc')
      -&gt;orderBy('month', 'desc')
      -&gt;get();
  </code></pre>
  
  <p>Which gives me table like this:</p>
  
  <pre><code>   array:3 [
        0 =&gt; {#275
          +"year": "2015"
          +"month": "10"
          +"month_name": "October"
          +"id": "3"
        }
        1 =&gt; {#274
          +"year": "2015"
          +"month": "9"
          +"month_name": "September"
          +"id": "1"
        }
        2 =&gt; {#273
          +"year": "2014"
          +"month": "8"
          +"month_name": "August"
          +"id": "1"
        }
      ]
  </code></pre>
  
  <p>How can I print it on my view like I described? 
  If I go through the array in views like this:</p>
  
  <pre><code>@foreach($links as $link)
      &lt;h3 class="text-uppercase"&gt;&lt;a href="{{ url('blog/'.$link-&gt;year) }}"&gt;{{ $link-&gt;year }}&lt;/a&gt;&lt;/h3&gt;
      &lt;p&gt;&lt;small class="blog_date"&gt;&lt;a href="{{ url('blog/'.$link-&gt;year.'/'.$link-&gt;month) }}"&gt;{{ $link-&gt;month_name }} ({{ $link-&gt;id }}) &lt;/a&gt;&lt;/small&gt;
  @endforeach
  </code></pre>
  
  <p>I tried to use foreach-loop in my Controller to create another array from DB-results where structure would be in correct form and I could just use foreach to  print it on the view, but couldn't get it work. </p>
  
  <p>I know I'm near the solution, but I'm still learning. Please someone tell me which is the best way to do this.</p>