Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

cklester's avatar

Three-Way Relationships

I've got a set-up where users can join groups and have a role in that group. I have three tables:

users, group_roles, groups

I have a ManyToMany relationship between the users and groups, because many users can be part of many groups, and groups can have many users.

The group_roles table defines role types, like "admin," "owner," "member," and the pivot table for users<->groups ("group_user") includes the "role" integer field, which references a record in group_roles.

I've got the users<->groups pivot working fine. What do I need to do to do something like this:

$users = User::find(auth()->id()); // get current user
$groups = $users->groups; // grab the groups he is a member of
// how do I get the name of the role, and not just the id?

I'm just not sure what the common approach is for this. I checked eloquent-relationships in the docs. Is it "Has Many Through?"

Anyway, I'm stuck. If anybody has clues or links, I'd be grateful.

0 likes
2 replies
cklester's avatar

I'm thinking of "inner joins."

How do I get the fields of two related tables, when each table uses the same field name?

For example, my Users table has a "name" field, and the Groups table has a "name" field. The join is overwriting one with the other, instead of giving me "Users.name" and "Groups.name."

I guess a good question is, how can I prepend the table name to the fields?

Please or to participate in this conversation.