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

Colin_Laws's avatar

How do you add to a pivot table that has multiple relationships?

I have a pivot table by the name of account_user_permissions that joins the accounts, users, and permissions tables together. I have already set up a custom pivot model class by the name of AccountUserPermission that defines a belongsTo relationship to my account, user, and permission models.

On my account, user, and permission models, I define the newPivot function, and check the type of the model passed in:

// This method is on my user model
public function newPivot(Model $parent, array $attributes, $table, $exists, $using = null)
{
    if ($parent instanceof Account) {
        return new AccountUserPermission($parent, $attributes, $table, $exists, $using);
    }

    return parent::newPivot($parent, $attributes, $table, $exists, $using);
}

I am trying to add to this pivot table when I create a new account, user, and permission, but I can't figure out how to add to it because there are three relationships on the pivot. Normally, you can just attach straight to a pivot table.

Am I implementing this correctly? I can't seem to get an answer out of anybody on this forum, I'm continuously ignored when it comes to pivot tables between multiple tables. Can someone please just tell me if I'm doing it wrong?

0 likes
5 replies
Colin_Laws's avatar

Okay, I just got this line to add a row to the pivot table:

$user->accounts()->attach($account->id, ['permission_id' => $permission->id]);

However, the created_at field is not being set when the relationship is added. What am I doing wrong here?

EventFellows's avatar

on Pivot relationships you have to set define additional columsn and timestamps explicitly like this ->withTimestaps().

See documentation for that.

For a many-to-many relationship you do NOT need the model for the pivot table, just create the blongsToMany on both models that you put into a relationship. Your third relation would then just be an additional column on that table.

So you might want to read up on that sort of relationships as your setup seems a bit 'uncommon'. You would probably want to avoid that as it makes things more complicated down the road if you do not stick with conventions.

Colin_Laws's avatar

The reason I did this was because I have a permissions table that is shared between the account_user_permissions table and the database_user_permissions table because they are the same object.

I don't see how it is really all that uncommon to have a table that defines a relationship between three tables, and needing to access a relationship and filter given the third relationship on the pivot.

For example:


// An arbitrary account
$account = App\Models\Account::first();

// To pull my 'owners' of the account, I do all of this leg work
// Doesn't seem right.
$owners = $account->users(function ($user) {
    return $user->pivot->permission->owner;
});

Should I seriously consider refactoring my database design to make it easier for laravel? That just doesn't seem like the right thing to do to me, because I was always taught to put my database into the most normalized form.

The permission object defines permissions that are applicable to both accounts and databases, so it makes sense to me to create it this way.

It looks like it's more of just a limitation of Eloquent, as Entity Framework handles these types of situations with no problem at all.

Colin_Laws's avatar

I just settled on adding the permissions to the intermediate tables, because of the potential for future modifications for the software that would introduce differences between the permissions for accounts and databases. This simplifies access to the permissions through Eloquent.

EventFellows's avatar

i guess i meant that the setup of your relations is kind of uncommon.

db structure itself seems fine to me.

Please or to participate in this conversation.