duanchen6423 2016-03-23 00:42
浏览 38
已采纳

多列的雄辩关系

So I have the following match table which contains the numbers of the teams that participated in that match. I want to set up a relationship with the teams which looks something like this:

Teams Table

| id | number |  name   |  etc  |
| 1  |  1234  | Example |  etc  |
| 2  |  2345  | Example |  etc  |

etc...

Matches Table

| id |  match  |  red1  | red2 | blue1 | blue2 |
| 1  |    1    |  1234  | 1710 |  673  | 2643  |
| 2  |    2    |  2345  | 1677 | 4366  | 246   |

etc...

I want to have something like $this->match->where("match", "=", "2")->first()->teams();.

I have tried using hasMany() but I can't seem to get to use the red1, red2, blue1, blue3 columns.

What I have tried:

class Matches extends Model
{
    protected $table = "match_table";

    protected $fillable = [
        "match_id",
        "time",
        "bluescore",
        "redscore",
        "red1",
        "red2",
        "red3",
        "blue1",
        "blue2",
        "blue3",
    ];

    public function teams()
    {
        return $this->hasMany("App\Models\Teams", "number", ["red1", "red2", "blue1", "blue2"]);
    }
}
  • 写回答

1条回答 默认 最新

  • duanhuanzhi6431 2016-04-04 21:18
    关注

    What I ended up doing was just looping through each column I wanted and then just returning a new Collection with the results in it.

    public function teams()
    {
        $result = [];
    
        foreach($this::$teamColumns as $column) {
            $result[] = $this->hasMany("App\Models\Teams", "number", $column)->first();
        }
    
        return new Collection($result);
    }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 企业资源规划ERP沙盘模拟
  • ¥15 树莓派控制机械臂传输命令报错,显示摄像头不存在
  • ¥15 前端echarts坐标轴问题
  • ¥15 CMFCPropertyPage
  • ¥15 ad5933的I2C
  • ¥15 请问RTX4060的笔记本电脑可以训练yolov5模型吗?
  • ¥15 数学建模求思路及代码
  • ¥50 silvaco GaN HEMT有栅极场板的击穿电压仿真问题
  • ¥15 谁会P4语言啊,我想请教一下
  • ¥15 这个怎么改成直流激励源给加热电阻提供5a电流呀