5

I've already posted this on the laravel forums but nobody was able to offer any help. Thought I'd post here for a second opinion.

I need a little advice/help with how to structure my models. Here's what I'm trying to do

A User can be a member of multiple Teams, but the user can fulfill a different Role on each team.

The idea being that the user sees a different set of data/features depending on the role they occupy on the team.

So I had thought about something like:

Users
id, name, email, etc...

Teams
id, name, description

Roles
id, name

Team Users
user_id, team_id, role_id

The team_members table would tie the 3 pieces together. Defining what teams a user belongs to and the role they have.

My questions are:

  1. Is this the best way to go about such a relationship
  2. How would I assign a user to a team with a role?
  3. Using Eloquent, how would I get a users role on a given team?
George G
  • 7,443
  • 12
  • 45
  • 59
Mcg1978
  • 464
  • 6
  • 19
  • I find myself in an almost identical scenario. If I may ask, how did you solve this as you haven't accepted any answers? – Iain Feb 15 '14 at 17:45
  • I sort of worked around it in the end, but the answer is to have a model for your pivot table. The pivot has a relationship with the 'role' model so you can access $user->pivot->role – Mcg1978 Feb 18 '14 at 13:18
  • 1
    Thanks, I arrived at the same conclusion. – Iain Feb 20 '14 at 01:52
  • 1
    @Mcg1978 mind showing an example? I'm new to laravel and I've been stuck on this problem for a while! – Helen Che Jan 02 '15 at 05:33

1 Answers1

2

From your scenario, the tables that you define looks fine, to assign a user to a team and his role, provided you create a model TeamMember for the table you can do something like:

//assign or update user team or role
$new_member = TeamMember::firstorCreate(array(
              'user_id' => $this->user->id,
              'team_id' => $team_id,
              'role_id' => $role_id,
              ));

//accessing the role of a user on a team
$role = TeamMember::where('user_id', '=', $user_id)
        ->where('team_id', '=', $team_id)->first()->role_id;

//get the name of the role
$role_name = Role::find($role)->name;
har2vey
  • 676
  • 6
  • 19
  • The edit queue is full, so a couple of remarks here: `firstOrCreate` is misspelled. Also, `firstOrCreate` does not assign or update a user team or role as described by the comment, but returns the first Team member that matches the user_id, team_id and role_id or creates a new teammember with those attributes. Also, the `$new_member` variable is misleading in that context, because it might very well be an existing member. – Lupinity Labs Oct 14 '20 at 00:55
  • `TeamMember::where('user_id', '=', $user_id)->where('team_id', '=', $team_id)->first()->role_id;` is better written `TeamMember::firstWhere([['user_id', $user_id], ['team_id', $team_id]])`. Also, to take into account the result might be null, you should wrap it in an `optional()` statement before accessing `role_id`. – Lupinity Labs Oct 14 '20 at 01:00
  • Finally, it is good practice to use camel-case variable names, i.e. `$newMember` instead of `$new_member` – Lupinity Labs Oct 14 '20 at 01:01