Laravel: many-to-many with shared table

可紊 提交于 2019-12-11 13:57:49

问题


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:


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).



来源:https://stackoverflow.com/questions/28923610/laravel-many-to-many-with-shared-table

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!