I have the following database structure for my application:
users
id | name | email | email_verified_at | password | remember_token
teams
id | owner_id | name | slug | description
team_user
id | team_id | user_id
roles
id | name
I have been able to create relationships between the Team and User models from the following:
Team.php
public function users() {
return $this->belongsToMany(User::class);
}
User.php
public function organisations() {
return $this->belongsToMany(Team::class);
}
A user can be part of multiple teams and should be able to have different roles within each of these teams, is it possible to achieve this by adding a role_id column to my team_user table instead of creating another Pivot table like the following:
team_user_role
id | team_id | user_id | role_id
If so, which Eloquent relationship should be created between the User within the Team and the Role? Could someone explain clearly the correct relationships as I am receiving SQL errors when trying to access the user's role from my Team model:
@foreach ($team->users as $user)
{{ $user->role }}
@endforeach