Laravel如何return整条数据的最高值
Laravel how to return a whole item data with the highest value
我怎样才能 return 整个项目数据,我已经使用 max 来获取最高值,但它只有 return 的值该字段不是整个项目
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 对您想要最大值的字段进行排序,然后 select 第一个结果。
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();
而不是使用 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();
我怎样才能 return 整个项目数据,我已经使用 max 来获取最高值,但它只有 return 的值该字段不是整个项目
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 对您想要最大值的字段进行排序,然后 select 第一个结果。
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();
而不是使用 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();