1

Im trying to get a result from a database, what I want to do is get a sum of a curton field then group the results then order by. Heres what I have, but it keeps giving me an error. The error im getting is:

Call to a member function orderBy() on double in

Heres my code:

$app->place->where('week_no', $week)->where('win_lose', 'win')->groupBy('username')->sum('number')->orderBy('number', 'ASC')->get();

Any help is much appreciated. Thanks

Mika Tuupola
  • 19,877
  • 5
  • 42
  • 49
Adam
  • 455
  • 1
  • 3
  • 18

1 Answers1

0

Your error comes from the sum() call. It returns the sum of the elements, so other calls will simply fails cause you have a number and not an object anymore.

I don't know your exact database schema, but I think that you can do what you want using:

$app->place
      ->select(DB::raw('SUM(number) as number'))
      ->where('week_no', $week)
      ->where('win_lose', 'win')
      ->groupBy('username')
      ->orderBy('number', 'ASC')
      ->get();
Davide Pastore
  • 8,678
  • 10
  • 39
  • 53