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.