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

标签 php laravel laravel-5 eloquent backend

如何返回整个项目数据,我使用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/

相关文章:

php - Yii CUserIdentity 与用户模型

php - Laravel belongsToMany 与 OR 条件

php - Laravel 注册。仅注册拥有来自特定域的电子邮件的用户

laravel - Cloudflare 错误 502 错误网关 - Nginx - Laravel5

php - 如何在 Mac 操作系统中为 Laravel 运行 Composer 命令?

php - 派生属性未从父构造函数初始化

php - 我应该为上传的文件设置什么文件权限

php - HTTP 查询字符串和 []

PHP mktime 和时区

php - Laravel 5.2 验证检查值是否不等于变量