代码之家  ›  专栏  ›  技术社区  ›  L. Fox

我在这里用的是什么样的Laravel雄辩的关系

  •  2
  • L. Fox  · 技术社区  · 7 年前

    我有一个板模型和一个管脚模型,每个板都包含所有管脚,每个管脚都放在每个板上。我很难弄清楚应该使用什么样的雄辩关系以及如何建立这种关系。

    如果需要,我可以共享任何代码。任何帮助都将不胜感激! 如果我的问题不清楚,请告诉我!提前感谢!

    1 回复  |  直到 7 年前
        1
  •  2
  •   rocambille r233967    7 年前

    每个电路板包含所有引脚,每个引脚位于每个电路板上

    所以 电路板可能与 许多的 引脚,和 pin可能与 许多的 董事会:这是 many to many relation

    所以 Board.php 可能如下所示:

    class Board extends Model
    {
        public function pins()
        {
            return $this->belongsToMany('App\Pin');
        }
    }
    

    Pin.php :

    class Pin extends Model
    {
        public function boards()
        {
            return $this->belongsToMany('App\Board');
        }
    }
    

    请注意,此处的数据库中需要3个表: boards ,则, pins 和数据透视表 board_pin 其中包含 board_id pin_id 列。