简体   繁体   English

Laravel Spatie Permission 多模型权限

[英]Laravel Spatie Permission Multi Models to Permission

I know how to assign permissions to user.我知道如何为用户分配权限。

class User extends Authenticatable
{
   use HasRoles;
}

$user->givePermissionTo('edit articles');

In this case, in model_has_permissions table will be inserted like this.在这种情况下,model_has_permissions 表将像这样插入。

permission_id=> $permission_id
model_type=> "App\Models\User"
model_id=> $user_id

I have Team Model and I want to give permission to Team too.我有团队模型,我也想授予团队权限。 This is what I did.这就是我所做的。

class Teamextends Authenticatable
{
   use HasRoles;
}

$team->givePermissionTo('edit articles');

I wanted this result,我想要这个结果,

permission_id=> $permission_id
model_type=> "App\Models\Team"
model_id=> $team_id

But it didn't work for me.但它对我不起作用。 Anyone has experience such as above things?有没有人有类似上面的经历? Should I make another table for it?我应该为它做另一张桌子吗? like team_has_permission ?team_has_permission

I found solution!我找到了解决方案!

class Team extends Authenticatable
{
   use HasRoles;
   protected $guard_name = 'web';
}

$team->givePermissionTo('edit articles');

This worked well.这工作得很好。 In db table, it saved like this.在db表中,它是这样保存的。

permission_id=> $permission_id
model_type=> "App\Models\Team"
model_id=> $team_id

Really awesome!非常棒!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM