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

StackBoot's avatar

Define Relationships for those models

I have this ER diagram with the three tables organizations users and roles. My question is how do I define the relationships between those, based on ER diagram I do have a pivot table which I do not understand how it works, any help would be appreciated ?

0 likes
8 replies
bobbybouwmann's avatar

What have you tried so far?

There are lots of good examples in the document, you can use those as a reference for setting up your models ;)

StackBoot's avatar

@bobbybouwmann so In user model I added

  public function organizations()
    {
        return $this->belongsToMany(Organization::class, 'user_id', 'organization_id');
    }

 
    public function roles()
    {
        return $this->belongsToMany(Role::class, 'user_id', 'role_id');
    }

and in role model :

    public function users()
    {

        return $this->belongsToMany(User::class);
    }
}

my migrations:

organization_migration


    public function up()
    {
        Schema::create('organizations', function (Blueprint $table) {
            $table->increments('id');
            $table->string('name', 255);
            $table->softDeletes();
            $table->timestamps();
        });

        Schema::create('organization_user', function (Blueprint $table) {
            $table->increments('id');
            $table->integer('organization_id')->unsigned();
            $table->integer('user_id')->unsigned();
            $table->integer('role_id')->unsigned();

            $table->foreign('role_id')->references('id')->on('roles');

        });
    }

and my user migration:

    public function up()
    {
        Schema::create('users', function (Blueprint $table) {
            $table->increments('id');
            $table->string('name');
            $table->string('email')->unique();
            $table->string('password');
            $table->rememberToken();
            $table->timestamps();
        });

    }

And role_migration:

    public function up()
    {
        Schema::create('roles', function (Blueprint $table) {
            $table->increments('id');
            $table->string('name', 255);
            $table->string('slug', 255);
            $table->softDeletes();
            $table->timestamps();
        });
    }

My question is now: do I need to define a relationship in organization model as well since most proppably I will access the user from organization ?

robrogers3's avatar

your missing a couple of things.

so lets be clear:

Organization and User: User:belongsToMany Organizations and Oranization:belongsToMany Users

Role and User User::belongsToMany Roles and Role::belongsToMany users

Role and Organization Role::belongsToMany organizations and Organization:belongsToMany roles

So you need 3 pivot tables:

  • organization_user
  • role_user
  • role_organization

This means your Role model is missing: public function organizations {$this->belongsToMany(Role::class)}

And of course you didn't post your Organization Model: you need roles, and users

so for example, to get the users for an organization: $organization->users

and to get the roles for an organization: $organization->roles

and the inverse for user model.

make sense?

StackBoot's avatar

@robrogers3 well that seems interesting now, the thing is that I use Role model to define user role, I don't need $organization->roles since that does not make sens in my case. An Organization belongsToMany Users while Users belongsToMany Roles and vice versa for Role-User. and the last pivot should be organization_role btw

robrogers3's avatar
Level 37

yeah, I thought the Organization roles was weird. but your ER diagram mislabels the pivot table.

so it should be role_user.

and you need a organization_user pivot table

and the answer to your question is yes.

a orginazion.user belongsToManyUsers and vice versa

robrogers3's avatar

If Organizations don't have roles. Than yeah, kill it. This means you wont be able to $organization->roles. But that seems weird anyway.1

StackBoot's avatar

@robrogers3 it turns out that users can have different roles on different orgs. So I should be able to get a specific user that belongs to a specific org and grab the role. I did solve that in a better way I think by having only one pivot table for three models, instead of having three pivot tables(kinda not good db design)! But thanks for the time

Please or to participate in this conversation.