0

我正在开发一个人们可以查看体育比赛的应用程序。这些表具有复合主键。以下是表的相关属性的样子:

| Game                      | Team        | League_Team    |
|---------------------------|-------------|----------------|
| id (PK)                   | id (PK)     | league_id (PK) |
| league_id (PK)            | name        | team_id (PK)   |
| home_id  (season_team_id) | ...         | season_team_id |
| guest_id (season_team_id) |             | ...            |
| ...                       |             |                |
| ...                       |             |                |

所以我的目标是让所有比赛都与特定的主客队一起进行......所以它应该看起来像这样Game::with('homeTeam')->with('guestTeam')->....;

怎么可能得到这种关系?我试过hasOneThrough了,但我不知道它如何与复合主键一起使用!

谢谢你的帮助!

编辑

$primaryKey除了属性之外,模型都是空的。

游戏模型

protected $primaryKey = ['id', 'league_id']; public function homeTeam() { // ... }

public function up()
{
    Schema::create('basketball_games', function (Blueprint $table) {
        $table->unsignedInteger('id');
        $table->unsignedInteger('league_id')->nullable();
        $table->dateTime('date');
        $table->unsignedInteger('home_id');
        $table->unsignedInteger('guest_id');


        $table->primary(['id','league_id']);

        // Foreign Keys
        $table->foreign('league_id')->references('id')->on('basketball_leagues');

        $table->timestamps();
    });
}

团队模型

空的

public function up()
{
    Schema::create('basketball_teams', function (Blueprint $table) {
        $table->increments('id');
        $table->string('name');
        $table->timestamps();
    });
}

联赛_球队

protected $primaryKey = ['league_id','team_id'];

public function up()
{
    Schema::create('basketball_league_teams', function (Blueprint $table) {
        $table->unsignedInteger('league_id');
        $table->unsignedInteger('team_id');
        $table->unsignedInteger('season_team_id');
        $table->string('name');
        $table->timestamps();

        $table->primary(['league_id','team_id']);

        // Foreign Keys
        $table->foreign('league_id')->references('id')->on('basketball_leagues');
        $table->foreign('team_id')->references('id')->on('basketball_teams');
    });
}
4

0 回答 0