How to Get Last 1, 3, 6, 12 Months Data in Laravel

To get 1, 3, 6, 12 months data in laravel apps; Through this tutorial, i am going to show you how to get the last 1, 3, 6, 12 months and last month in laravel apps.

How to Get Last 1, 3, 6, 12 Months Data in Laravel

Follow the below given to get last 1, 3, 6, 12 month data in laravel:

  • To Get Last 3 Months Data in Laravel
  • To Get Last 6, 12 Months Data in Laravel
  • Get Last 15 Days & 30 Days Data in Laravel

To Get Last 3 Months Data in Laravel

To get 3 month data or record from database tables in laravel; as follows:

       $items = Item::select('*')
                        ->whereBetween('created_at', 
                            [Carbon::now()->subMonth(3), Carbon::now()]
                        )
                        ->get();
  

To Get Last 6, 12 Months Data in Laravel

To get 6, 12 month data or record from database tables in laravel; as follows:

       // for last 6 month data
       $items = Item::select('*')
                        ->whereBetween('created_at', 
                            [Carbon::now()->subMonth(6), Carbon::now()]
                        )
                        ->get();


       // for last 12 month data
       $items = Item::select('*')
                        ->whereBetween('created_at', 
                            [Carbon::now()->subMonth(12), Carbon::now()]
                        )
                        ->get();


  

Get Last 15 Days & 30 Days Data in Laravel

To get the last 15 days and last 30 days records from the database in laravel:

$last_15_days = User::where('created_at','>=',Carbon::now()->subdays(15))->get(['name','created_at']);

 $last_30_days = User::where('created_at','>=',Carbon::now()->subdays(30))->get(['name','created_at']);

Recommended Laravel Tutorials

Be First to Comment

Leave a Reply

Your email address will not be published. Required fields are marked *