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:
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
Can you give a little more information about where and how you are trying to create this relationship?
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:
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:
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?
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.