Get last week data in Laravel

You can get last week data in Laravel. Sometimes you have to get the records which are created at previous week of the current month, in that case you can use whereBetween() method to get the last week records from the database table.

Answers 2
  • Get last week (Saturday to Sunday) created users record in Laravel

    --PATH routes\web.php
    Route::get('/get-last-week-users', function(){
        $previous_week = strtotime("-1 week +1 day");
        $start_week = strtotime("last sunday midnight",$previous_week);
        $end_week = strtotime("next saturday",$start_week);
        $start_week = date("Y-m-d",$start_week);
        $end_week = date("Y-m-d",$end_week);
    
        $users = App\Models\User::whereBetween('created_at', [$start_week, $end_week])->get();
        dd($users);
    });
    
    0

    Output:

    ^ Illuminate\Database\Eloquent\Collection {#1437 ▼
      #items: array:1 []
    }

    You can get the last week created users record in Laravel. You have to copy/paste code in your web.php file under route directory and visit /get-last-week-users url to get the users record.

  • Get previous 7 days data using today() and subDays() method on Carbon in Laravel

    $date = \Carbon\Carbon::today()->subDays(7);
    $users = App\Models\User::where('created_at','>=',$date)->get();
    dd($users);
    
    0
  • Back to code snippet queries related laravel