moataz elamen
moataz elamen

Reputation: 35

How can i orderby and groupby in one query in laravel

I'm facing a problem when trying to order after using groupBy I wanna sort the grouped entries but its not working here is what I was trying to do

$errors = UserErrors::all()->groupBy('user_id')->orderBy('id');

Upvotes: 1

Views: 679

Answers (1)

Yudiz Solutions
Yudiz Solutions

Reputation: 4459

$errors = UserErrors::groupBy('feild_name')->orderBy('feild_name','DESC')->get();

Example as below

$query->where('feild_name', '=', 'active')
    ->groupBy('feild_name')
    ->orderBy('date','DESC')
    ->get();

Upvotes: 1

Related Questions