EvergreenWebSystems
5 months ago
214
8
Laravel

User Relationships

Posted 5 months ago by EvergreenWebSystems

What would be the best way to construct my database tables to allow for the following relationships.

  • A project has an owner
  • A project has many collaborators
  • A collaborator can have different permission levels per project

Using Eloquent I would like to be able to show a single list of projects per user, for example:

USER A

  • Project 1 (Owner)
  • Project 2 (Owner)
  • Project 3 (Collaborator - Editor)
  • Project 4 (Collaborator - Read only)

Some questions:

  1. If I add a 'user_id' column in the projects table and a pivot table for 'many-to-many' relationships between users and projects will I easily be able to use an Eloquent query to display a single list of projects? I.e. because I will need to check both the user_id column in the projects table and the pivot table.
  2. Would it be better to use a single pivot table and assign a permission level as part of the pivot table, so on a relationship by relationship basis I could define the link between the user and the project and then somehow also add a permission type method?

I am new to Laravel, and just seeking clarification/advice on best practices for this kind of thing.

Thanks

Please sign in or create an account to participate in this conversation.