Я работаю над приложением, где люди могут просматривать спортивные игры. Таблицы имеют составные первичные ключи. Вот как выглядят соответствующие атрибуты таблиц:
| 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();
});
}
League_Team
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');
});
}