PenAndPapers
PenAndPapers

Reputation: 2108

Laravel how to return a whole item data with the highest value

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
]

Upvotes: 4

Views: 3663

Answers (4)

Sibasankar Bhoi
Sibasankar Bhoi

Reputation: 589

Only change 'max' to 'orderBy'.

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

Upvotes: 0

AddWeb Solution Pvt Ltd
AddWeb Solution Pvt Ltd

Reputation: 21681

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!

Upvotes: 0

Rahul
Rahul

Reputation: 2474

Instead of using max use orderBy

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

Upvotes: 1

Teun
Teun

Reputation: 926

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

Upvotes: 4

Related Questions