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

To get the last 7,15,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 days & months data or record 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 7,15,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']);

Conclusion

That’s it, you have learned how to get last 7,15,30 days and 1,2,3,6,9,12 month data in laravel app.

Recommended:-Laravel Try Catch

AuthorDevendra Dode

Greetings, I'm Devendra Dode, a full-stack developer, entrepreneur, and the proud owner of Tutsmake.com. My passion lies in crafting informative tutorials and offering valuable tips to assist fellow developers on their coding journey. Within my content, I cover a spectrum of technologies, including 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. Whether you're starting out or looking for advanced examples, I provide step-by-step guides and practical demonstrations to make your learning experience seamless. Let's explore the diverse realms of coding together.

Leave a Reply

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