group by with like in laravel 8

$results = User::select(\DB::raw('YEAR(date) as year, COUNT(id) as amount'))
    ->groupBy(\DB::raw('YEAR(date)'))
    ->get();

So, we’re using DB::raw() for all select statement, also repeating the same condition in groupBy(). Here’s the visual result for my dummy seeded data:



Leave a Reply