Eloquent Relationship Pivot Table with 3 Model

260 Views Asked by At

i'm a little confused, i need to implements a user role according to an trade unions that the user belongs. The user can belongs to a lot of trade unions but his role can be different.

The migrations will be:

+-----------+  +-----------+  +---------------+  +-------------------+
|   users   |  |   roles   |  |  tradeunions  |  |  user_role_trade  |
+-----------+  +-----------+  +---------------+  +-------------------+
|   id      |  |   id      |  |    id         |  |     user_id       |
|   name    |  |   name    |  |    name       |  |     role_id       |
+-----------+  +-----------+  +---------------+  |     trade_id      |
                                                 +-------------------+

What would be the best way to do it? I've worked with pivot table with two models, but with three, I'm a bit confused Thanks a lot !

1

There are 1 best solutions below

0
Erubiel On

You can make a UserRoleTrade Model for your pivot table.

Then implement the required functions like the relationship between the User Model and this Model.