Php Laravel雄辩-获得另一张桌子的计数

Php Laravel雄辩-获得另一张桌子的计数,php,laravel,eloquent,eloquent-relationship,Php,Laravel,Eloquent,Eloquent Relationship,我有一个能言善辩的模特“运动员”,还有另一张桌子上的表演。每个运动员都有0到多场比赛。我想获得每个运动员的最佳成绩(个人最佳成绩),如果运动员还没有任何表现,则为空 我的运动员模型: class Athlete extends Model { // I would like to do something like public $personalBest = max(performances) - the highest perfomance /** *

我有一个能言善辩的模特“运动员”,还有另一张桌子上的表演。每个运动员都有0到多场比赛。我想获得每个运动员的最佳成绩(个人最佳成绩),如果运动员还没有任何表现,则为空

我的运动员模型:

class Athlete extends Model
{

    // I would like to do something like
    public $personalBest = max(performances) - the highest perfomance

    /**
     * The table associated with the model.
     *
     * @var string
     */
    protected $table = 'athletes';

    /**
     * The primary key associated with the table.
     *
     * @var string
     */
    protected $primaryKey = 'id';

    /**
     * Indicates if the model should be timestamped.
     *
     * @var bool
     */
    public $timestamps = false;

    /**
     * Get the performances for the Athelete post.
     *
     * @return HasMany
     */
    public function performances()
    {
        return $this->hasMany('App\EloquentModels\Performance', 'athlete_id', "id");
    }
}
我希望每个运动员都能有最好的表现。希望它真的有意义

我想必须在某个地方回答,但我没找到。如果我找不到,我很抱歉

Performances table
id(int) year(int) performance(float)
-------------------------------------
1       2000      257.3 
2       2001      227.3 
总而言之。过帐生成的最终原始查询:

select [athletes].[first_name], [athletes].[last_name], MAX(performance) AS personal_best
from [athletes] 
left join [performances] on [athletes].[id] = [performances].[athlete_id] 
group by [athletes].[id], [athletes].[first_name], [athletes].[last_name] 
order by [personal_best] desc
我们应该用它来做这项工作

$athletes= App\Athlete::withCount('performances')->get();

foreach ($athletes as $athlete) {
    echo $athlete->performances_count;
}
如果您希望获得最高性能,可以执行以下操作

 $athletes= App\Athlete::all();

    foreach ($athletes as $athlete) {
        echo $athlete->performances->pluck('performance')->max();
    }
DB('athletes as ath')::leftJoin('performaces as perf', 'ath.id', 'perf.athelete_id')->select(e.athelete.id, max(perf.performace) as max_performace)->orderBy('max_performace'); 
差不多

select e.athelete.id, max(perf.performace) as max_performace
from atheletes ath
  left join performaces perf on ath.id = perf.athelete_id
group by ath.id, max_performace
可能是

 $athletes= App\Athlete::all();

    foreach ($athletes as $athlete) {
        echo $athlete->performances->pluck('performance')->max();
    }
DB('athletes as ath')::leftJoin('performaces as perf', 'ath.id', 'perf.athelete_id')->select(e.athelete.id, max(perf.performace) as max_performace)->orderBy('max_performace'); 
如果需要,您可以使用“按最大性能排序”

我想你也可以简单地使用

echo $athlete->performances->max('performance');

我不需要计算性能,我需要选择一个最高的性能(或空的运动员有0个性能)。很抱歉,如果描述没有提供足够的信息。@MarekBarta你能显示演示数据吗?我已编辑了我的问题,但希望查看演示数据以使其准确无误。性能是浮动的。。我编辑了我的问题以显示绩效table@MarekBarta你能试用我编辑过的答案吗?使用Pull获取
性能
集合并检索
max()
。我刚刚试着在我的一个项目中获得最昂贵的食物,我得到了正确的价值。我想编辑后的答案就可以了。。。但是只是性能测试,当我只需要一个最好的性能时,难道不可能不加载所有的性能吗。。这可能是对每个运动员表现的恐惧