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

Continuum's avatar

3 Way Many To Many

I've been thinking about a database schema problem for what feels like forever and am now seeking a more experienced brain to point me in a good direction.

I'd like to have three tables,

Users Organisations Roles

But have a many to many relationship between all three. That is ... A user can belong to many organisations, and have one or many roles within that user-organisation pair. (Developer & Designer for example), but not necessarily have the same exact roles with another organisation.

A pivot table seems great as an initial start as far as the user-organisation pair is concerned, but as soon as I try get a user-organisation-role going, things seem to fall apart.

Could someone please post some simplistic schema structure and potentially a model sample establishing the eloquent methods which will be used to make it work ?

0 likes
7 replies
pmall's avatar

within that user-organisation pair

You have to create a model to represent this user-organisation pair. Then :

  • User has many UserOrganisation
  • UserOrganisation belongs to an User
  • Organisation has many UserOrganisation
  • UserOrganisation belongs to an Organisation
  • Many to many relationship with pivot table between Role and UserOrganisation

Then display the user roles :

$user = User::find(1);

foreach ($user->user_organisations as $user_organisation)
{
    $user_organisation->organisation->name;

    foreach($user_organisation->roles as $role)
    {
        $role->name;
    }

}

It also works starting from an organisation and listing users with their roles within the organisation.

1 like
Continuum's avatar

@bobby - Thanks, I have taken a look at that and I'm not convinced its the correct solution - In the docs the example is Countries, Posts, Users - This goes under the assumption that a User belongs to only one country and that that link is set on the Users table (country_id). In my example, Countries would be organisations, here, my User can belong to Many organisations, but when he does - his roles change.

I'm really quite new at this, so am I missing something ?

pmall's avatar

HasManyThrough doesnt work with many to many relationships. Look at my answer above.

mattbryanswan's avatar

I've seen another similar solution, also requiring a fourth model. In that other implementation, however, the fourth model was the glue... so User, Organization, and Role, all connected to that fourth model. That felt clunky to me.

I like pmall's way better, I think. Creating the abstraction between just 2 (User and Organization) and then using that abstract model to create the relationship to roles. This feels like it mirrors the way I'd want to go at it programmatically anyway... first, let's make sure the user is a member of the organization... then let's check his or her roles to see if the action they are trying to take it allowed.

Thanks guys!

Please or to participate in this conversation.