Hayanno
Hayanno

Reputation: 182

How to add pivot table to pivot table

My DB is something like that : participant <=> event

I have a table "event_participant" to link the two others.

Now I want a new table : "Eat", that adds dinner's date for each day of an event for a participant.

I imagine table "Eat" like that : event_id, participant_id, date

But maybe it's better to do so : event_participant_id, date

Or maybe there's another way; you tell me.

I use Eloquent for Laravel 5.1 but any SQL answer could help.

Upvotes: 1

Views: 38

Answers (1)

ArtisanBay
ArtisanBay

Reputation: 1041

Assuming your participant table

id  |  participant_fname   |  participant_lname  |  created_at  |  updated_at
    |                      |                     |              |

Assuming your event table

id  |  event_name  |  created_at  |  updated_at
    |              |              | 

Assuming your event_participant table

id  |  participant_id    |  event_id  |  created_at  |  updated_at
    |                    |            |              |   

As you have mentioned for the new Eat table, you could save the primary key of event_participant table. By doing so you could fetch the Event and Participant related details by doing a JOIN query. Both this table would be Pivot tables.

Eat table

id  |  event_participant_id   |  created_at  |  updated_at
    |                         |              |   

Eloquent Relations

Laravel Relationships

Within your model you could then include the relationships between each tables to fetch values using Eloquent Relations

enter image description here Hope this is helpful.

Upvotes: 1

Related Questions