【问题标题】:Laravel Dynamic DB RelationLaravel 动态数据库关系
【发布时间】:2020-07-20 19:24:37
【问题描述】:

我没有找到任何解决方案,所以我在这里问它..

Match.php 
id , teams           name
1    5,6(string)   kkr vs mi
Team.php 
id  name 
5   kkr
6   mi

Team 的 id 与 Match 中的团队相关,因此解决方案很简单.. 但问题是一场 Match 可以有 3/4+ 没有团队所以我必须让它动态......

【问题讨论】:

  • 最简单的解决方案是在您的数据库中引入数据透视表。例如 team_matchesidteam_idmatch_id 并通过 hasManyThrough 获取所有关系

标签: php mysql sql laravel laravel-6


【解决方案1】:

根据@maki10,可行的解决方案是

public function teams() {
      return $this->hasManyThrough(
         'App\model\Team',
         'App\model\MatchTeam',
         'match_id',
         'id',
         'id',
         'team_id'
      );
    }

【讨论】:

    猜你喜欢
    • 2020-05-11
    • 2015-02-07
    • 2014-04-22
    • 2018-11-08
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多