Get last week data in Laravel

Updated at 18-Nov-2021, By samar

Get last week data in Laravel

In this session, we’ll try our hand at solving the "Get last week data in Laravel" puzzle by using the computer language.

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.
  • 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();


    ^ 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();

Back to code snippet queries related laravel