Get Last 3, 6, 12 Months Data in Laravel

Get Last 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 3, 6, 12 Months and Last Month 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 Tutorials

Recommended:-Laravel Try Catch

AuthorAdmin

My name is Devendra Dode. I am a full-stack developer, entrepreneur, and owner of Tutsmake.com. I like writing tutorials and tips that can help other developers. I share tutorials of PHP, Python, Javascript, JQuery, Laravel, Livewire, Codeigniter, Node JS, Express JS, Vue JS, Angular JS, React Js, MySQL, MongoDB, REST APIs, Windows, Xampp, Linux, Ubuntu, Amazon AWS, Composer, SEO, WordPress, SSL and Bootstrap from a starting stage. As well as demo example.

Leave a Reply

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