Php Laravel如何返回具有最高值的整项数据
如何返回整个项目数据,我使用了max来获取最高层的值,但它只返回字段的值,而不是整个项目的值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)
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();