Laravel 5.7中3个表之间的关系

时间:2019-01-21 09:46:49

标签: laravel relationship

我有三个表:

PLAYERS with a team_id

TEAMS

SCORES with a player_id 

问题是:

我的球队有得分得分的球员,我想对球队进行排名。因此,从根本上来说,要获得球员的最高分,并且如果一个团队有几个球员,就可以得出总分。

例如:

团队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');
}   

}

3 个答案:

答案 0 :(得分:0)

首先,您应该在hasMany模型上定义一个指向Player模型的Score函数。然后,您可以通过执行Player来查询$player->scores()->max('value')的最佳得分(假设$value是保存Score模型中实际得分值的属性)。

有了这些代码后,就可以使用该代码段定义an accessor,这样您就可以执行$player->bestScore之类的操作来获得玩家的最高得分。

然后可以通过一种查询玩家最佳成绩的方式,在指向HasMany模型的Team模型上定义Player关系。获取团队得分的查询类似于$team->players->sum('max_score')。您还可以为此属性定义一个变量,以便您可以像$team->score这样访问此代码段。

最后,要订购所有团队,将是:

  return Team::all()->sortByDesc('score');

答案 1 :(得分:0)

尝试这种简单方式

首先选择特定团队的所有球员,例如

$players=PLAYERS::where('team_id',1)->get();//1 is a sample value pass your team_id

然后使用foreach迭代每个玩家ID并获得最高得分

foreach($players as $player)
{
   $score=SCORES::where('player_id',$player)->orderBy('score', 'desc')->value('score'); // gets only the highest score of the player
   $total=$total+$score;//Sum of the highest score
}

然后,如果要插入表格中,则表示

TEAMS::create(['team_id'=>1,'score'=>$total]);

希望有帮助:)

答案 2 :(得分:0)

如果有人遇到相同的问题,我找到了解决方法:

static function getDailyTeams(int $limit = 10)
{        
    $callback = function($query) {
        $query->whereDate('scores.created_at', date('Y-m-d'));
    };
    $player = function($query) use ($callback) {
        $query->whereHas('scores', $callback)->with(['scores' => $callback]);
    };
    $teams = Team::with(['players' => $player])->get()->sortByDesc(function ($team) {
        return $team->players->sum('best_score');
    }
    );
    return $teams;  
}
相关问题