Skip to content
Advertisement

Laravel – How can I query months and yearly dates?

I am new to Laravel and not really sure how to do this.

I record dates in my database as start_day in my times table.
Date is persisted with the following format: 2019-10-03

Right now all I have is:

$months = DB::table('times')->where('start_day', '=', 2019)

How should I select data for each month of the year?

Advertisement

Answer

use whereYear

use CarbonCarbon;

$months = DB::table('times')->whereYear('start_day', (string) Carbon::now()->year)->get();

If you want to select months only then

$months = DB::table('times')->select(DB::raw('MONTH(start_day) month')->whereYear('start_day', (string) Carbon::now()->year)->get();
User contributions licensed under: CC BY-SA
4 People found this is helpful
Advertisement