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:
- Data integrity (if all phone numbers are in one common table it's easy to verify duplicate phone numbers are not entered)
- Binding to more complex models (replace
PhoneNumber
withImage
and now you have a lot more data to deal with)