title: Laravel: reading available months and years from timestamp to populate a <SELECT>
body: <p>I have a films table which a timestamps column CREATED_AT. THe format is:</p>
  
  <pre><code>name            created_at
  Pocahontas      2016-12-28 22:09:24
  </code></pre>
  
  <p>I know how to select films from the table added this current month</p>
  
  <pre><code>    $year_month_film = DB::table('films')
          -&gt;select('films.*')
          -&gt;whereMonth('films.created_at', '=', Carbon::now()-&gt;month)
          -&gt;orderBy('films.created_at', 'desc')
          -&gt;get();
  </code></pre>
  
  <p>But now I want to let the user to select dynamically a list of films added on any month of any year (see the image):</p>
  
  <p><a href="https://i.stack.imgur.com/fY8h3.jpg" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/fY8h3.jpg" alt="Selected all films from december 2016"></a></p>
  
  <p>I built two  in the HTML view in order to select dynamically the films added any month of any year.</p>
  
  <pre><code>&lt;div class="pull-right"&gt;
              &lt;select&gt;
                &lt;option value="all" selected&gt;Months&lt;/option&gt;
                &lt;option value="January"&gt;January&lt;/option&gt;
                &lt;option value="February"&gt;February&lt;/option&gt;
                &lt;option value="March"&gt;March&lt;/option&gt;
                ...
              &lt;/select&gt;              
              &lt;select&gt;
                &lt;option value="all" selected&gt;Years&lt;/option&gt;
                &lt;option value="2011"&gt;2011&lt;/option&gt;
                &lt;option value="2014"&gt;2014&lt;/option&gt;
                &lt;option value="2016"&gt;2016&lt;/option&gt;
                ...
              &lt;/select&gt; 
          &lt;/div&gt; 
  </code></pre>
  
  <p>The questions are:</p>
  
  <ol>
  <li>I did not add films every month and every year, therefore I need to know which months and which years (independent from each other) exists in the database by reading the column "created_at". And then pass to the view. But I have not idea how to do it using Carbon.</li>
  <li>How could I grab the selected month and year in the , pass then to the controller and make a dynamic search instead just the current month that I posted at the beginning?</li>
  </ol>
  
  <p><strong>EDIT:</strong></p>
  
  <p>Just to update to somebody who may be useful:
  I have removed the duplicated months by adding a line to the code apported by Yohanan Baruchel and the code looks now like that:</p>
  
  <pre><code>$available_years_with_months = [];
  
          $all_year_and_months = DB::table('films')
             -&gt;select('*')
             -&gt;orderBy('films.created_at', 'desc')
             -&gt;get()-&gt;each(function($film) use (&amp;$available_years_with_months) {
               $date = Carbon::parse($film-&gt;created_at);
               $year = $date-&gt;format('Y');
               $month = $date-&gt;format('F');
               $available_years_with_months[$year][] = $month;
              $available_years_with_months[$year] = array_unique($available_years_with_months[$year]);
  
             });
  </code></pre>