Roles on pivot table

What I am trying to do:
Roles are handled by spatie/laravel-permission.
I have two models with different roles in my project. One is User and one is OrganisationUser (pivot table between the User & Organisation). I want to show and edit this role in the relationship manager that I made for the OrganisationResource.

What I did:
I tried to make a text column with the name 'pivot.roles.name' because just 'roles.name' works for the UserResource. Eventually I made an attribute called rolesNames in OrgansationUser by doing 'pivot.rolesNames' I am able to show roles correct in the relationshipmanager. But this does not fix editing roles.

My issue/the error:
I can't seem to edit roles in the relationship manager. If I make a select the same way I did in the UserResource, it takes the roles from user. I want it to take the roles from the pivot table (OrgansationUser).

Code:
UsersRelationshipManager.php
 Tables\Columns\TextColumn::make("pivot.rolesNames")
                    ->label(__("labels.roles"))
                    ->badge()
                    ->formatStateUsing(fn($state) => __('roles.' . $state))


OrganisationUser.php
<?php
class OrganisationUser extends Pivot
{
    use HasRoles;

    // Config files do this for the User model. We do it by setting it manually here.
    protected $guard_name = 'web';

    // The table needs an id so that it can be referenced by the model_has_roles table.
    public $incrementing = true;

    public function user(): BelongsTo
    {
        return $this->belongsTo(User::class)->with('roles');
    }

    public function organisation(): BelongsTo
    {
        return $this->belongsTo(Organisation::class)->with('roles');
    }

    public function organisationRoles(): BelongsToMany {
        return $this->roles();
    }

    protected function rolesNames(): Attribute
    {
        return Attribute::make(
            get: fn () => $this->roles->pluck('name'),
        );
    }
}
Was this page helpful?