Andreas Baran
Andreas Baran

Reputation: 659

Laravel 4 - group by and search by MySQL YEAR()

I have a problem in a query I'am making in Laravel.

My query looks like this:

<!-- Select all event in this year -->
$thisYear = test::where('id', '=', $id)
        ->where('YEAR(start)', '=', date("Y"));

<!-- Group all events by year -->
$years = test::where('id', '=', $id)
        ->groupBy('YEAR(start)');

In the first code I would like to get all the events that there is in this year (2013)

In the second I would like to group all events in Years.

Hopes somebody can help.

Regards, Andreas

Upvotes: 3

Views: 2814

Answers (1)

Collin James
Collin James

Reputation: 9280

You are looking for DB::raw(). I think the following should work.

$thisYear = test::where('id', '=', $id)
    ->where(DB::raw('YEAR(start)'), '=', date('Y'));

Upvotes: 8

Related Questions