1

How would I go about ordering a sum in Laravel

Currently, using

Auth::user()->activities()->groupBy('attempts_id')->sum('points');

Will only give me the first row. I need to order by that sum in descending order and grab the first item.

Using ->get() at the end results in an error: Call to a member function get() on a non-object

lukasgeiter
  • 147,337
  • 26
  • 332
  • 270
gin93r
  • 1,551
  • 4
  • 21
  • 39

1 Answers1

6

Try this:

Auth::user()
    ->activities()
    ->groupBy('attempts_id')
    ->orderByRaw('SUM(points) DESC')
    ->first();
lukasgeiter
  • 147,337
  • 26
  • 332
  • 270