Hello devs,
In this get current year data in laravel example tutorial, I will show you that query of how to get current year data in laravel using carbon. I will use carbon to show you the way of fetching current year data in laravel. If you don't know how to fetch current year data using carbon in laravel then this example is for you.
Let assume we have a date
column in our table and we have to fetch current year data from this table. I will show you an eloquent query using carbon to current year data in laravel.
Sometimes we need to show that kind of data to make a report in our application. So Let's see the example query of current year data. I will use whereYear
to fetch current month data in laravel.
Example query:
use Carbon\Carbon;
use App\Models\User;
use Illuminate\Support\Facades\Route;
Route::get('/', function () {
return User::select('*')
->whereYear('created_at', Carbon::now()->year)
->get();
});
Hope it can help you.
#laravel #eloquent-tips