У меня есть три стола:
PLAYERS with a team_id
TEAMS
SCORES with a player_id
Дело в том:
В моих командах есть игроки, у которых есть очки, и я хотел бы составить рейтинг команд.Таким образом, в основном набирайте лучший результат по игрокам и суммируйте его, если в команде несколько игроков.
Например:
В TEAM A есть игрок 1 и игрок 2. У игрока 1 есть 3 очка (например, 300, 150 и 500), и я хотел бы оставить только лучший (итак 500).
Ты хоть представляешь, как я могу это сделать?Поскольку нет прямой связи между командами и счетами или между игроками и счетами, я не понимаю, как я могу установить связь между этими 3.
Спасибо за вашу помощь!
РЕДАКТИРОВАТЬ
Оценка модели
class Score extends Model
{
protected $fillable = [
'value', 'player_id'
];
public function player()
{
return $this->belongsTo('App\Player');
}
public function players_scores()
{
return $this->hasManyThrough('App\Team', 'App\Player');
}
}
Модель игрока
class Player extends Model
{
protected $fillable = [
'name','email','team_id'
];
/**
* Get the team of the player
*/
public function team()
{
return $this->belongsTo('App\Team');
}
/**
* Get the scores of the player
*
* @return \Illuminate\Database\Eloquent\Relations\HasMany
*/
public function scores()
{
return $this->hasMany('App\Score');
}
/**
* Get the maximum score from the player
*
* @return mixed
*/
public function getBestScoreAttribute()
{
return $this->scores->max('value');
}
}
Модель команды
class Team extends Model
{
protected $fillable = ['name','logo'];
protected $appends = ['score'];
public function players()
{
return $this->hasMany('App\Player');
}
/*
* Collect all the team players scores
*/
public function players_scores()
{
return $this->hasManyThrough('App\Score', 'App\Player');
}
public function scores()
{
return $this->hasMany('App\Score');
}
/*
* Sum the score of all team players to get the team score
*/
public function getScoreAttribute()
{
return $this->players_scores->sum('value');
}
}