How to get last month records in Laravel

Updated at 17-Nov-2021, By samar

How to get last month records in Laravel

Hello everyone, in this post we will look at how to solve "How to get last month records in Laravel" in programming.

You can get last month records from a table in Laravel. Laravel provides the whereMonth() method which helps you to get the records from table which are created in previous month.
  • Get last month records from users table in Laravel 8

    User::whereMonth('created_at', '=', \Carbon\Carbon::now()->subMonth()->month)->get();
    
    //Demo code
    //routes\web.php
    Route::get('/get-last-month-records', function(){
        $users = App\Models\User::whereMonth('created_at', '=', \Carbon\Carbon::now()->subMonth()->month)
                                ->get();
        dd($users);
    });
    

    You can get the records from users table which are created at last month. Simply visit /get-last-month-records and you can get the records from users table which are created at previous month.

Back to code snippet queries related laravel