Hi everyone! I have four tables:
Event - id, name
Corporation - id, name
Job - id, corporation_id, name
RegisteredJob - id, event_id, corporation_id, job_id.
As you can see, RegisteredJob table has three foreign keys - representing jobs registered for a job fair event. I think I should use belongsToMany for relationships... but the example Laravel documentation is using only two tables.
Has anyone tried belongsToMany with 3 foreign keys? Did it go well or did you find it tricky with all these pivot where... pivot sync... and etc?
If belongsToMany is particularly for two tables with many-to-many relationships only... Should I refactor RegisteredJob table something like the following?
Event - id, name
Corporation - id, name
Job - id, corporation_id, name
// New table storing corporations attending an event.
RegisteredCorporation - id, event_id, corporation_id.
RegisteredJob - id, registered_corporation_id, event_id, job_id.
The reason why I'm keeping three foreign keys in one table is that it reduces the amount of codes I need to write... for example,
// To find a corporation name and an event name... I need to use nested with functions...
$registered_job = RegisteredJob::with('registered_corporation.corporation')->with('registered_corporation.event')....
// With one table with three foreign keys... I can simply use..
$registered_job = RegisteredJob::with('corporation', 'event')....
To recap ::
-
Is belongsToMany still good for a table with more than 2 foreign keys?
-
If not, should I refactor registered job table cuz my datbase schema has an issue in the first place?
Thanks