title: Get last 7 days data from controller laravel
body: <p>I am trying to create a function that get the last seven days date and count the common date.</p>
  <p>Something like this</p>
  <pre><code>column1     column2
  10/28/2020      5
  10/27/2020      0
  10/26/2020      6
  10/25/2020      2
  10/24/2020      1
  10/23/2020      0
  10/22/2020      2
  </code></pre>
  <p>but when getting the date of last seven days the date getting off.
  <a href="https://i.stack.imgur.com/6Vjra.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/6Vjra.png" alt="enter image description here" /></a></p>
  <p>This is the code:</p>
  <pre><code>        $today = Carbon::today();
          $date_array = array();
          $date_count = array();
  
          $i = 0;
          while ($i &lt; 7) {
              array_push( $date_array, $today-&gt;subDays($i)-&gt;format('Y-m-d H:i:s') );
              $i++;
          }
  
          if(! empty( $date_array ) ){
              foreach($date_array as $date){
                  $date_count = CampaignHistory::where( 'created_at', '&gt;', $date )-&gt;get()-&gt;count();
              }
          }
  
          echo json_encode($date_array );
  </code></pre>