Assign permissions to Pivot Table #1782
Replies: 8 comments
-
You just have to add a pivot on the tables "roles" and "model_has_permissions", and then overwrite the relationship in the User model "public function roles(): MorphToMany" and "permissions(): MorphToMany", i use session for save the new pivot on login, and later i use the session function for add the pivot on user relationship with whereraw, is that what whas loking for? |
Beta Was this translation helpful? Give feedback.
-
@erikn69 |
Beta Was this translation helpful? Give feedback.
-
@kaaaaaaaaaaai add a pivot field, and on login authenticated i put that id on session Now overwrite relations on user model and add a where for session id laravel-permission/src/Traits/HasRoles.php Lines 37 to 49 in a6e4122 public function roles(): BelongsToMany
{
return $this->morphToMany(
config('permission.models.role'),
'model',
config('permission.table_names.model_has_roles'),
config('permission.column_names.model_morph_key'),
'role_id'
)->where(config('permission.table_names.model_has_roles').'.empr_id',session('empr_id'));
} Permissions laravel-permission/src/Traits/HasPermissions.php Lines 40 to 52 in a6e4122 public function permissions(): BelongsToMany
{
return $this->morphToMany(
config('permission.models.permission'),
'model',
config('permission.table_names.model_has_permissions'),
config('permission.column_names.model_morph_key'),
'permission_id'
)->where(config('permission.table_names.model_has_permissions').'.empr_id',session('empr_id'));
} Now works with a pivot id based on session, with that example also you can made custom roles for each pivot id |
Beta Was this translation helpful? Give feedback.
-
And maybe duplicate of #1634 |
Beta Was this translation helpful? Give feedback.
-
@erikn69 Thanks for the details, I tried writing your code to see if it worked, but it didn't work with my understanding. Can you help me?Would it work in my situation? My situation is ex.) User B, when the subdomain is "sample", has the Writer role I'm hoping for a result like this.
|
Beta Was this translation helpful? Give feedback.
-
@kaaaaaaaaaaai potato patato, the same with diferent ids Then, all the code on user public function roles(): BelongsToMany
{
return $this->morphToMany(
config('permission.models.role'),
'model',
config('permission.table_names.model_has_roles'),
config('permission.column_names.model_morph_key'),
'role_id'
)->where(config('permission.table_names.model_has_roles').'.community_id',session('community_id'));
}
public function permissions(): BelongsToMany
{
return $this->morphToMany(
config('permission.models.permission'),
'model',
config('permission.table_names.model_has_permissions'),
config('permission.column_names.model_morph_key'),
'permission_id'
)->where(config('permission.table_names.model_has_permissions').'.community_id',session('community_id'));
} Now, User A can has the Writer role when the subdomain is "sample", and when the subdomain is "test", the owner role, and repeat the same role or has diferents on other subdomain because Also, in addition maybe you have to overwrite It can be improved, but it works, nobody wants to do a PR for this, nor do they provide information, maybe @freekmurze can better understand the idea and add it to the package Also using |
Beta Was this translation helpful? Give feedback.
-
@kaaaaaaaaaaai i have the same problem, did erikn69's solution works for you? |
Beta Was this translation helpful? Give feedback.
-
Yes, overriding the trait's relations in your app is a suitable way to accomplish this. |
Beta Was this translation helpful? Give feedback.
Uh oh!
There was an error while loading. Please reload this page.
-
Hi, how to Use Spatie Laravel-permission Package for Pivot Table ?
Beta Was this translation helpful? Give feedback.
All reactions