如何返回整个项目数据,我使用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 对您想要最大值的字段进行排序,然后选择第一个结果。
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();
关于php - Laravel 如何返回具有最高值的整个项目数据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44537535/