Using Spatie's Laravel-permission, is there a way to assign roles to a pivot table?

For example, if I have a User that is associated with a Tenant, but also Groups within Tenants, can I assign a role so that it gets saved in the model_has_roles table so that it has something like:
role_id = 549839
model_type = App\Models\Tenant_User
model_id = 53093

role_id = 549840
model_type = App\Models\Group_User
model_id = 83553
role_id = 549839
model_type = App\Models\Tenant_User
model_id = 53093

role_id = 549840
model_type = App\Models\Group_User
model_id = 83553
It continuously wants to just associate the role with the User model, and not based specifically on the specific relationship. And how would I try to define that relationship in the Resource?
4 Replies
cheesegrits
cheesegrits10mo ago
Can you give a little more information about where and how you are trying to create this relationship?
Clay Bitner
Clay Bitner10mo ago
I've tried it in a few ways. First is within the UsersRelationManager within the GroupResource RelationManagers. Another place is within the UserResource when creating a user within the Tenant Admin panel. Inside the form, it looks like this:
return $form->schema([
Forms\Components\Section::make('User Details')
->schema([
Forms\Components\TextInput::make('first_name')
->required(),
Forms\Components\TextInput::make('last_name')
->required(),
Forms\Components\TextInput::make('username')
->required(),
Forms\Components\TextInput::make('email')
->required()
->email(),
Forms\Components\TextInput::make('password')
->required()
->password()
->maxLength(255)
->rule(Password::default())
->dehydrateStateUsing(fn ($state) => \Hash::make($state))
->visible(fn ($livewire) => $livewire instanceof Pages\CreateUser),
Forms\Components\CheckboxList::make('tenant_user.roles')
->relationship('roles', 'name'),
// ->preload(true)
]),
]);
return $form->schema([
Forms\Components\Section::make('User Details')
->schema([
Forms\Components\TextInput::make('first_name')
->required(),
Forms\Components\TextInput::make('last_name')
->required(),
Forms\Components\TextInput::make('username')
->required(),
Forms\Components\TextInput::make('email')
->required()
->email(),
Forms\Components\TextInput::make('password')
->required()
->password()
->maxLength(255)
->rule(Password::default())
->dehydrateStateUsing(fn ($state) => \Hash::make($state))
->visible(fn ($livewire) => $livewire instanceof Pages\CreateUser),
Forms\Components\CheckboxList::make('tenant_user.roles')
->relationship('roles', 'name'),
// ->preload(true)
]),
]);
Would you have any advice on how to approach it, though? Is there something I should be changing in the user model? I have these functions defining the groups and tenants within the User model:
public function groups(): BelongsToMany
{
return $this->belongsToMany(Group::class)->using(GroupUser::class)->withTimestamps();
}

public function tenants(): BelongsToMany
{
return $this->belongsToMany(Tenant::class)->using(TenantUser::class)->withTimestamps();
}
public function groups(): BelongsToMany
{
return $this->belongsToMany(Group::class)->using(GroupUser::class)->withTimestamps();
}

public function tenants(): BelongsToMany
{
return $this->belongsToMany(Tenant::class)->using(TenantUser::class)->withTimestamps();
}
cheesegrits
cheesegrits10mo ago
I'm a little confused as to what you are trying to do here. Surely if you are assigning your users to groups, then you need to be assigning them to groups on that form, not roles. Then you assign the roles for that group elsewhere. So you would have as more or less static set of permissions for a given group (like 'Editors' might have 'can-edit-thing' and 'can-delete-thing'), and in the User form you would add them to that group (the user-group pivot), you wouldn't be assigning roles?
Clay Bitner
Clay Bitner10mo ago
Within the app, there are these groups where people can have chats and discussions and share pictures and the like. A user might have the role of "moderator" in one group (which allows them to add pictures, add new posts, remove posts, edit comments), but not another group, even if they are in both groups. In another group they may just have the role of "user" which allows them to view posts and pictures, for example. But a group exists within an organization (tenant), so there are multiple layers. From a UX perspective, when you add a user to a group, you'd want to define their role at the same time - because that's where the association lies.
Want results from more Discord servers?
Add your server
More Posts
Calculated State Typed property Filament\Forms\Components\Component::$container must not be accessedIn my UserResource class, I'm attempting to populate a toggle based on the soft delete flag on the uIs there a way to limit the amount of relations that can be selected when using a relationshipIs there a way when clicking Add Product and they select a product , you only allow a max of 3 to beFiltered DataI want to export only filtered table data to a csv , For instance, 10 records are in the database, aHow to speedup livewire update response on edit page relation manager?I have edit page with 2 relation manager, and every change tab between 2 relation manager, the speedGetting a `required` validation error when running the `make:filament-widget` commandIn the latest version (`v3.0.63`), running `artisan make:filament-widget` without using a resource, Radio button options stores first digit onlyI have a radio button as follows ``` Radio::make('age') ->options([ '0-1 years', '1-2 years',Multiple Simple (modal) resources in a single PageHi Team, first time Filament User here. Sorry, if this is a really simple solution. I my app, I wanMake additional column in table based from the sum of two other columns?Hi, is there a nicer way to make an additional column in a table which is based of the sum of two otTable query string, how can i put in the query string url the individual column searchLike im using the https://filamentphp.com/docs/3.x/tables/advanced#query-string in my table, but i hTeams or TenancyIs there a way or feature that tenancy is searchable and/or preloaded? Assumption, there are 20 of t