gpt4 book ai didi

php - Laravel 如何返回具有最高值的整个项目数据

转载 作者:行者123 更新时间:2023-12-04 02:04:29 25 4
gpt4 key购买 nike

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

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/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com