title: Laravel - Model filter date field by month
body: <p>I have a method that can receive a parameter of <strong>"month"</strong> and another of <strong>"year"</strong>, you can receive both or only one of them.</p>
  
  <p>In the case of <em>only receiving the month</em> I want to make a query that I look for in a field that is called <strong>"created_at"</strong> just looking for the <strong>month</strong> of that field date</p>
  
  <p>At the moment I use this code but when doing a like it does not do it correctly</p>
  
  <pre><code>        else if ($request-&gt;has('month')) {
          $month = $request-&gt;input('month');
          $currentTimestamp = date_create_from_format('n', $month)-&gt;getTimestamp();
          $currentDate = date('m', $currentTimestamp);
          $filters['updated_at'] = $currentDate;
          unset($filters['month']);
      }
  
          $cars = Car::where(function($query) use($filters) {
          foreach ($filters as $column =&gt; $value) {
              if ($column === 'updated_at') {
                  $query-&gt;where($column, 'LIKE', '%'.$value.'%');
                  continue;
              }
              $query-&gt;where($column, 'LIKE', '%'.$value.'%');
          }
      })-&gt;orderBy('updated_at', 'desc')-&gt;get();
  </code></pre>