【发布时间】:2016-01-23 17:58:07
【问题描述】:
我正在努力考虑使用一种干净的方式在 Laravel 中设置三个模型之间的工作关系。
有大量的比赛,其中总是包含2支球队,每支球队都有5名球员。
注意:玩家不是用户,所以每场比赛都会产生新的球队和球员。
匹配项
class Match extends Model
{
public function teams()
{
return $this->hasMany('App\Team');
}
}
团队
class Team extends Model
{
public function players()
{
return $this->hasMany('App\Player');
}
public function match()
{
return $this->belongsTo('App\Match');
}
}
玩家
class Player extends Model
{
public function team()
{
return $this->belongsTo('App\Team');
}
}
我的问题是我在可爱的 Laravel 文档中找不到使用具有两个索引的数据库表的解决方案。
具体:比赛条目有一个获胜者和一个较松的球队。如何用雄辩的方式告诉 Laravel?
匹配项
Schema::create('matches', function (Blueprint $table) {
$table->increments('id');
$table->integer('winner_team_id')->index();
$table->integer('looser_team_id')->index();
团队
Schema::create('teams', function (Blueprint $table) {
$table->increments('id');
$table->integer('player_1')->index();
$table->integer('player_2')->index();
$table->integer('player_3')->index();
$table->integer('player_4')->index();
$table->integer('player_5')->index();
玩家
Schema::create('players', function (Blueprint $table) {
$table->increments('id');
这种方法“不干净”还是我错过了文档中明显的一些东西,比如模型的更多基于关系的类?
我是否必须为 manyToMany-relationships-approach 设置更多表,例如“match_teams”和“team_players”?
【问题讨论】: