Laravel 5.1 - Pivot table between three existing physical tables

I am new to Laravel 5.1 and am currently working on a project to personally familiarize myself with Laravel.

I have many tables in my application, but I'm stuck on a certain plural relationship between 3 tables (which Pivot tables might need to use).

So I have 3 tables:

  • Roles
  • Users
  • Projects

The following rules apply to relationships:

  • A user can have from 0 to many projects
  • Access to the project is possible by many different users.
  • The user gets some rights to a specific project depending on its role.
  • The user has an โ€œactiveโ€ project from which the application receives its data. Users can change their "active" project from the list of projects.

(permissions are associated with roles and allow a user with a specific role to perform a specific set of actions in a project)

At first, I had users โ†” Roles that worked well (as far as the code is concerned), but the role defined for the user allowed me to make a predefined set of things for each project (which is not flexible enough for me).

I already saw another post with approx. the same title as mine , but I'm afraid it cannot satisfy my needs, because the properties are stored in the intersection table, although in my case I already have a physical table.

+4
source share
1 answer

Create a model for the pivot table, this will make it easier to work with.

ProjectAssignee:

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

Then you can define the following relationships:

  • Project: hasMany (ProjectAssignees)
  • ProjectAssignee: belongs to (Project), belongs to To (User), belongs to To (Role)
  • User: hasManyThrough (Project, ProjectAssignee), hasMany (ProjectAssignee)
+1
source

All Articles