duai8153 2015-03-08 06:03
浏览 70
已采纳

Laravel:共享表多对多

I have Locations model which hasMany Employees -- similarly Employees belongsTo Locations

This is nice and works well, but then I looked at adding PhoneNumbers. Either a Location or an Employee could have a phone number (office numbers versus personal numbers)

Logically:

Locations hasMany PhoneNumbers (multiple office lines) and Employees hasMany PhoneNumbers (home / cell ?)

However when you create a hasMany relationship like this in Laravel it adds a field to the PhoneNumbers table. So we now have two fields: location_id and employee_id

I can get this to work if I make location_id and employee_id nullable, like so:

+----+--------------+-------------+-------------+
| id |    number    | location_id | employee_id |
+----+--------------+-------------+-------------+
|  1 | 800-555-0123 |      1      |     null    |
|  2 | 800-555-0124 |      1      |     null    |
|  3 | 800-555-0125 |      1      |     null    |
|  4 | 859-555-0199 |     null    |       1     |
                     ...

However this doesn't scale very well if I add new entities that can possess phone numbers (customers? job applicants? suppliers?)

How can I create multiple separate many-to-many relationships with the same secondary table?

Note: In this example I could just create a phone_number field on each individual tables (locations.phone_number, employees.phone_number, etc) however I wish to avoid this for two reasons:

  1. Data integrity (if all phone numbers are in one common table it's easy to verify duplicate phone numbers are not entered)
  2. Binding to more complex models (replace PhoneNumber with Image and now you have a lot more data to deal with)
  • 写回答

1条回答 默认 最新

  • douan8473 2015-03-08 06:59
    关注

    You're looking for Laravel's polymorphic relationship. Instead of creating a new field for each related table, you have two fields: related id and related type.

    On both your Location and Employee model, add the following relationship:

    public function phones()
    {
        return $this->morphMany('PhoneNumber', 'phonable');
    }
    

    On your PhoneNumber model, add the following relationship:

    public function phonable()
    {
        return $this->morphTo();
    }
    

    On your phone_numbers table, add two new fields: phonable_type and phonable_id. In a migration, these fields are added with the morphs() method: $table->morphs('phonable');

    Once everything is setup, your data would look like this:

    +----+--------------+-------------+---------------+
    | id |    number    | phonable_id | phonable_type |
    +----+--------------+-------------+---------------+
    |  1 | 800-555-0123 |      1      |    Location   |
    |  2 | 800-555-0124 |      1      |    Location   |
    |  3 | 800-555-0125 |      1      |    Location   |
    |  4 | 859-555-0199 |      1      |    Employee   |
                         ...
    

    With this setup, you can make any model you want phonable just by adding a morphOne() or morphMany() relationship to it.

    Additionally, the relationship attributes will generate the correct model related to the type. Given the data above:

    var_dump(PhoneNumber::find(1)->phonable); // will dump Location object
    var_dump(PhoneNumber::find(4)->phonable); // will dump Employee object
    

    The documentation on polymorphic relationships can be found here (4.2) or here (5.0).

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

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