Thursday, December 26, 2024
Google search engine
HomeLanguagesHow to Get Last 1, 3, 6, 12 Months Data in Laravel

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

To get the last 30 days and 3, 6, 12 months data in laravel; Through this tutorial, you will learn how to get the last 1, 3, 6, 12 months and last month in laravel.

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

Use the following laravel eloquent queries to get last 3, 6, 12 month data and last 30 days 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

Use the following laravel eloquent query to get 3 month data or record from database tables in laravel; as shown below:

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

To Get Last 6, 12 Months Data in Laravel

Use the following laravel eloquent query to get 6, 12 month data or record from database tables in laravel; as shown below:

       // 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

If You want to get the last 15 days and last 30 days records from the database in laravel. Use the below given laravel eloquent query:

$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 Try Catch

RELATED ARTICLES

Most Popular

Recent Comments