Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/laravel/11.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/blackberry/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php Laravel如何返回具有最高值的整项数据_Php_Laravel_Laravel 5_Eloquent_Backend - Fatal编程技术网

Php Laravel如何返回具有最高值的整项数据

Php Laravel如何返回具有最高值的整项数据,php,laravel,laravel-5,eloquent,backend,Php,Laravel,Laravel 5,Eloquent,Backend,如何返回整个项目数据,我使用了max来获取最高层的值,但它只返回字段的值,而不是整个项目的值 public static function getTeamLeaders($competitionId, $teamId) { if($teamId){ $ppg = self::where('competitionId', $competitionId) ->where('teamId', $teamId)

如何返回整个项目数据,我使用了max来获取最高层的值,但它只返回字段的值,而不是整个项目的值

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;
        }
    }
结果:

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

您可以使用orderBy对希望最大值的字段进行排序,然后选择第一个结果

文件:


不要使用
max
而是使用
orderBy

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

我认为您应该更新您的代码,如:

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;
        }
    }

希望这项工作为你

仅将“max”更改为“orderBy”

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