4

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 :

  • Roles
  • Users
  • Projects

The following rules apply to the relationships :

  • A user can have 0 to many projects
  • A project can be accessed by many different users
  • A user is granted some rights on a specific project depending on his role
  • A user has an "active" project which is the one the application gets its data from. Users can change their "active" project from the projects list.

(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.

Willi Mentzel
  • 27,862
  • 20
  • 113
  • 121
  • One thing that helped me was to use a pivot table that had a extra column in it. So in your case possibly a pivot table for users and projects and then to include a role_id column in that pivot table. Can a user only have one "active" project at a time? – Mhluzi Bhaka Jul 31 '15 at 10:14
  • Also you say "I already have the physical tables.", what tables are those ? Can you show us the layouts? – Mhluzi Bhaka Jul 31 '15 at 10:17
  • In fact I have an intersection table defined as per the Laravel norm. ie. project_user migration. If I add role_id on the pivot table I don't know I I'll be able to retrieve the role that a user has for a project and also you're right, one user can only have 1 active project at a time. So I think I'll be able to add maybe role_id on the intersection table and also define which project is active at a given time. I'll try something like this and post a snippet once I succeed so that others will benefit from it. – Charles Dubant Jul 31 '15 at 18:54
  • 1
    Have a look at the answer to this question: http://stackoverflow.com/questions/30222003/laravel-5-synching-an-extra-field-via-pivot - as that might give you some direction on getting the role_id – Mhluzi Bhaka Aug 02 '15 at 07:31
  • Thank you, I'll check this and update once I implement a working solution to help others. – Charles Dubant Aug 05 '15 at 14:14
  • just for clarity. A user can only have one project - can a project have more than one user? Also a user has a role - can the user have more than one role? Presumably you're ok to amend your existing tables to modify/add fields? – Ray Aug 10 '15 at 14:10

1 Answers1

1

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)
Vincent Mimoun-Prat
  • 28,208
  • 16
  • 81
  • 124