How to Get Last N Days Records in Laravel

Hello Artisan

In this tutorial i am going share with you that how to get last 7 days record in laravel? Actually it's not last 7 days, it should be last N days records. If you know this query then you can fetch any data between two dates. See the example code of how to get last one month data in laravel from below example.

You can also learn how to get last one month data in laravel from this tutorial. Sometimes in our Laravel application we need to show Last N days records like last 7 days records, last 30 days record or last days record. You will learn all of them from this tutorial.

Example code : 

\App\Model::whereBetween('date', [
               \carbon\Carbon::now()->subdays(30)->format('Y-m-d'),
               \carbon\Carbon::now()->subday()->format('Y-m-d')
            ])->get();

 

Read also : How to Use groupBy() having() and havingRaw() with DB::raw in Laravel ?

 

Hope this fetching data using laravel date tutorial will help you.