Charles Dubant
Charles Dubant

Reputation: 53

Laravel 5.1 - Pivot table between 3 existing physical tables

I'm new to Laravel 5.1 and I'm currently working on a project to familiarize with Laravel on my own.

I have many tables in my application but I'm getting stuck on a specific multiple relation between 3 tables (that maybe should use Pivot tables).

So, I have 3 tables :

The following rules apply to the relationships :

(permissions are linked to roles and allow a user with a specific role to perform a defined set of actions on a project)

I initially had a Users <-> Roles which worked well (as far as code is concerned) but the role defined for a user permitted to do a predefined set of things on every project (which is not flexible enough to me).

I already saw another post with approx. the same title as mine but I'm afraid this can't address my needs because the properties are held on the intersection table though in my case, I already have the physical tables.

Upvotes: 4

Views: 239

Answers (1)

Vincent Mimoun-Prat
Vincent Mimoun-Prat

Reputation: 28541

Make a model for your pivot table, this should make it easier to work with.

ProjectAssignee:

  • project_id
  • user_id
  • role_id (means on project X, user Y has role Z)

Then you can define following relationships:

  • Project: hasMany(ProjectAssignees)
  • ProjectAssignee: belongsTo(Project), belongsTo(User), belongsTo(Role)
  • User: hasManyThrough(Project, ProjectAssignee), hasMany(ProjectAssignee)

Upvotes: 1

Related Questions