Php Laravel-具有复合主键的HasOneThrough
我正在开发一个应用程序,人们可以在其中查看体育游戏。这些表具有复合主键。以下是表的相关属性的外观:Php Laravel-具有复合主键的HasOneThrough,php,laravel,eloquent,has-one-through,Php,Laravel,Eloquent,Has One Through,我正在开发一个应用程序,人们可以在其中查看体育游戏。这些表具有复合主键。以下是表的相关属性的外观: | Game | Team | League_Team | |---------------------------|-------------|----------------| | id (PK) | id (PK) | league_id (PK) | | league_id (PK)
| 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']代码>
公共函数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();
});
}
团队模式
空的
联赛队
protected$primaryKey=['league\u id','team\u 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');
});
}
请编辑问题并发布所有三个模型及其表迁移。我已将所有内容添加到问题中@你有什么建议吗?谢谢
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');
});
}