Laravel

Laravel – get current month and year data

In this blog, we will learn about how to get current month and year data in Laravel, we will use the laravel eloquent to get our data.

If you want to know more about the laravel eloquent, then you can read the details from here

To achieve our goal we will the PHP date function. you can learn more about the PHP date function via this link.

For the current month, we will use whereMonth and for the current year we will use whereYear.

The below code will help you to get the current month and year in Laravel.

User::whereMonth('created_at', date('m'))
->whereYear('created_at', date('Y'))
->get();

The above code will give you the entire column detail, if you want to get only the selected column detail for the current month and year then you can use the below code.

User::whereMonth('created_at', date('m'))
->whereYear('created_at', date('Y'))
->get(['name','created_at']);

This code will give you the name and created_at column value.

Shaiv Roy

Hy Myself shaiv roy, I am a passionate blogger and love to share ideas among people, I am having good experience with laravel, vue js, react, flutter and doing website and app development work from last 7 years.

Related Articles

Leave a Reply

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

Back to top button