我有两个表:锦标赛和用户
在锦标赛模型中,我有一个BelongToMany关系
public function competitors()
{
return $this->belongsToMany('App'User')
->withPivot('tournament_id', 'confirmed')
->withTimestamps();
}
现在,我想从竞争对手的结果中获得最新的5个结果()
这看起来很容易,但我不知道如何按照雄辩的风格来做。
例如:
$latest = $tournament->competitors()->latest(5);
问题是竞争对手()结果不是一个基本模型,它是一个连接,所以我不能在模型中写函数。。。
知道吗?
您可以通过在数据透视表的列前加上pivot_来轻松地按数据透视表列排序,例如:
$latest = $tournament->competitors()->orderBy('pivot_created_at', 'desc')->get();