【问题标题】:Laravel Eloquent multiple relationship in a football game scheduleLaravel Eloquent 足球比赛日程中的多重关系
【发布时间】:2018-05-27 13:35:08
【问题描述】:

我是 Laravel 和 Eloquent 的新手,我正在尝试制定足球比赛计划。
现在我有 4 个表(带有一些示例条目):

团队(所有团队)

+---------+-----------+
| team_id | team_name |
+---------+-----------+
|       1 | Arsenal   |
|       2 | Chelsea   |
+---------+-----------+

比赛(所有比赛)

+----------------+------------------+
| competition_id | competition_name |
+----------------+------------------+
|              1 | Premier League   |
+----------------+------------------+

schedule(比赛日程)

+----+----------------+----------+--------------+--------------+-----------+-----------+
| id | competition_id | matchday | home_team_id | away_team_id | home_goal | away_goal |
+----+----------------+----------+--------------+--------------+-----------+-----------+
|  1 |              1 |        1 |            1 |            2 |         3 |         2 |
|  2 |              1 |        2 |            2 |            1 |         0 |         3 |
+----+----------------+----------+--------------+--------------+-----------+-----------+

schedule_teams(将日程 teamid 与比赛 ID 和 schedule_team_id 上的球队 ID 匹配)

+----+------------------+----------------+----------+
| id | schedule_team_id | competition_id | teams_id |
+----+------------------+----------------+----------+
|  1 |                1 |              1 |        1 |
|  2 |                2 |              1 |        2 |
+----+------------------+----------------+----------+

这是我目前的课程:

Schedule.php

public function competition()
{
   return $this->belongsTo(Competition::class, 'competition_id', 'competition_id');
}

Competition.php

public function schedule()
{
   return $this->hasMany(Schedule::class, 'competition_id', 'competition_id');
}

$id = \request('competition_id');
$schedule = Schedule::where('competition_id', $id)->with('competition')->get();

我从日程表中获得了带有主客场 ID 的日程表。 现在的问题是,如何通过 schedule_teams 表从 team 表中获取特定的主客场 ID,例如 home_team_id = 1:
home_team_id (=1) -> schedule_team_id (=1) 和 Competition_id (=1) -> 球队(阿森纳)

我希望集合中的时间表和相关团队的数据输出到刀片中。

谁能帮助或给我改进足球数据库的提示?

【问题讨论】:

  • 就像一个注释。将主键作为id 在您的表上被广泛认为是最佳实践。例如,在您的竞争表中,您不需要competition_id。否则会让人感到困惑。

标签: php mysql laravel eloquent


【解决方案1】:

您应该利用hasManyThrough 关系。

如果你创建说Schedule\Team,然后像下面这样。

public function schedule() {
    $this->belongsTo(Schedule::class, 'schedule_id');
}

public function team() {
    $this->belongsTo(Team::class, 'team_id');
}

现在在您的Schedule 课程中,您可以拥有以下内容。

public function teams() {
    $this->hasManyThrough(Team::class, Schedule\Team::class, 'schedule_id');
}

还应注意,您的日程安排团队不需要competition_id。既然一个队是属于一个赛程的,属于比赛的,你就可以这样搞定。

如果您还希望您的 Team 了解其日程安排,您可以将其添加到 Team

public function schedules() {
    return $this->hasManyThrough(Schedule::class, Schedule\Team::class);
}

Schedule\Team 类本质上变成了数据透视表的美化表示,但将其作为模型,您可以在未来对其进行扩展。它还有助于保持一切整洁。

希望这是有道理的。

【讨论】:

  • 太好了,它让我走上了正确的道路!谢谢!我将 teams() 编辑为 hometeams() 和 awayteams() 以获取两支球队,现在我可以使用$game->hometeams->first()->team_name 获取主客场球队,希望这是正确的方式?
猜你喜欢
  • 1970-01-01
  • 2015-05-19
  • 2022-07-11
  • 2020-11-01
  • 2022-01-12
  • 1970-01-01
  • 2015-04-16
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多