我正在使用Eloquent ORM,我试图找出俱乐部最成功的运动,基于比赛中的胜利。
我有表clubs
,tournaments
,sports
和tournament_results
,每个表都由模型(Club
,Tournament
,{{1}表示},Sports
)与关系。我表格中的相关栏目:
TournamentResult
,clubs
,tournaments
每个都包含sports
,id
name
:tournament_results
,id
,tournament
,club
。
关系:
place
Club
与HasMany
有ClubResults
TournamentResult
BelongsTo
Tournament
BelongsTo
Club
Tournament
HasMany
TournamentResults
BelongsTo
Sport
Sport
HasMany
Tournament
Club -> TournamentResults -> where place = 1 -> Tournaments -> get n (e.g. 3) most common sports in this set
有$club = Club::findOrFail($id);
$club->tournamentResults()->where('place', 1)->with('tournament.sport');
我对获得理想结果的想法是这样的:
CMake Error at cmake/modules/FindEigen3.cmake:42 (file):
file failed to open for reading (No such file or directory):
/usr//usr/include/eigen3/Eigen/src/Core/util/Macros.h
到目前为止我所拥有的是:
/usr/
但现在我不知道如何继续。有人可以帮我找到解决方案吗?
答案 0 :(得分:1)
将此关系添加到您的Club
型号:
public function wonTournaments() {
return $this->belongsToMany(Tournament::class, 'tournament_results')
->wherePivot('place', 1);
}
然后你可以这样做:
$topSport = $club->wonTournaments->groupBy('sport_id')
->sortByDesc(function($tournaments) {
return $tournaments->count();
})
->first()->first()->sport;