4

How can I return the whole item data, I've used max to get the hieghest value but it only returns the value of the field not the whole item

public static function getTeamLeaders($competitionId, $teamId) {
        if($teamId){
            $ppg = self::where('competitionId', $competitionId)
                    ->where('teamId', $teamId)
                    ->get()
                    ->max('Points');

            $apg = self::where('competitionId', $competitionId)
                    ->where('teamId', $teamId)
                    ->get()
                    ->max('Assists');

            $fgpg = self::where('competitionId', $competitionId)
                    ->where('teamId', $teamId)
                    ->get()
                    ->max('FieldGoals');

            $data = ['ppg' => $ppg, 'apg' => $apg, 'fgpg' => $fgpg];
            return $data;
        }
    }

Result:

array:3 [▼
  "ppg" => 15.18
  "apg" => 3.76
  "fgpg" => 12.04
]

4 Answers 4

4

You can use orderBy to sort on the field that you want the max of, and then select the first result.

docs: https://laravel.com/docs/5.4/queries#ordering-grouping-limit-and-offset https://laravel.com/docs/5.4/queries#retrieving-results

$ppg = self::where('competitionId', $competitionId)
                ->where('teamId', $teamId)
                ->orderBy('Points', 'desc')
                ->first();
Sign up to request clarification or add additional context in comments.

Comments

1

Instead of using max use orderBy

self::where('competitionId', $competitionId)
                    ->where('teamId', $teamId)
                    ->orderBy('Points', 'desc')
                    ->first();

Comments

0

I think you should update your code like :

public static function getTeamLeaders($competitionId, $teamId) {
        if($teamId){
            $ppg = self::where('competitionId', $competitionId)
                    ->whereRaw('Points = (select max(`Points`) from table_name)')
                    ->where('teamId', $teamId)
                    ->get();


            $apg = self::where('competitionId', $competitionId)
                    ->whereRaw('Assists = (select max(`Assists`) from table_name)')
                    ->where('teamId', $teamId)
                    ->get());

            $fgpg = self::where('competitionId', $competitionId)
                    ->whereRaw('FieldGoals = (select max(`FieldGoals`) from table_name)')
                    ->where('teamId', $teamId)
                    ->get();

            $data = ['ppg' => $ppg, 'apg' => $apg, 'fgpg' => $fgpg];
            return $data;
        }
    }

Hope this work for you!

Comments

0

Only change 'max' to 'orderBy'.

$something= self::where('competitionId', $competitionId)
            ->where('teamId', $teamId)
            ->orderBy('Points', 'desc')
            ->first();

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.