Laravel – get current month and year data

get current month and year in laravel

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.

5 1 vote
Article Rating

Do you want to hire us for your Project Work? Then Contact US.
Subscribe
Notify of
guest
1 Comment
oldest
newest most voted
Inline Feedbacks
View all comments
trackback

[…] You can also read: how to get current year and month data in laravel […]

1
0
Would love your thoughts, please comment.x
()
x