1

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');

  • 1) You havn't included an error message or anything actually demonstrating the problem. 2) If you're grouping by `user_id`, then there will be one row per `user_id` in the results, which means there wouldn't be an `id` to order by. 3) You haven't included a `select` to express how you're aggregating the rows. ***Conclusion:*** Make your query work *without* the `orderBy`, and only *then* move on to adding the `orderBy`, perhaps starting with this as an example: https://stackoverflow.com/questions/18533080/laravel-eloquent-groupby-and-also-return-count-of-each-group – MatBailie Apr 14 '21 at 15:06

1 Answers1

1
$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();
Yudiz Solutions
  • 4,216
  • 2
  • 7
  • 21