Level 11
Solution:
public function roles()
{
return $this->belongsToMany(Role::class, 'role_team_user', 'team_user_id', 'role', 'id', 'name');
}
Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.
I want to know how this relation works if I have alternative keys.
I like to write the relation "a team_user belongsToManyRoles" in Model TeamUser.php, but I don't know how. I try this:
public function roles()
{
return $this->belongsToMany(Role::class, 'role_team_user', 'role', 'team_user_id', 'id', 'name');
}
This returns roles() empty while I expect data in it. Why?
Solution:
public function roles()
{
return $this->belongsToMany(Role::class, 'role_team_user', 'team_user_id', 'role', 'id', 'name');
}
Please or to participate in this conversation.