简体   繁体   English

如何将 model 绑定到 Spatie 角色?

[英]How to bind model to the Spatie role?

Is it possibe to attach specific model to the role in Spatie laravel permissions ?是否可以将特定的 model 附加到Spatie laravel 权限中的角色?

I need to get model depends role.我需要获得 model 取决于角色。 For example if user has role doctor I need to get Doctor model. The same for clinic role.例如,如果用户具有医生角色,我需要获得医生 model。诊所角色也是如此。

Its a little hard to know what you are hunting for without knowing how you've set up the architecture underneath this, but you can get to the Doctors... not sure about binding directly though.在不知道如何设置其下的体系结构的情况下很难知道您在寻找什么,但是您可以找到医生……虽然不确定是否直接绑定。

I assume you have created a Spatie role and assigned it to a Doctor class (which I assume is an extended User or similar, and is using HasRoles ):我假设您已经创建了一个 Spatie 角色并将其分配给 Doctor class(我假设它是一个扩展的 User 或类似的,并且正在使用 HasRoles ):

$role = Role::create(['name' => 'doctor']);

Grab a doctor and assign the role as normal:抓住医生并正常分配角色:

$doctor = Doctor::find(xxx);
$doctor->assignRole($role);

I think the below may help:我认为以下内容可能会有所帮助:

$role= Role::findByName('doctor');
$roleClass= $role->getModel();

For the Docs with role doctor对于角色医生的文档

$docs= Doctor::role('doctor')->get();

If you are looking to distinguish between class (Doctor, User), you'll need to run an extra command on a $doc (or even a superset like User::role collection) like get_class but this will depend on how you've set the query and flow up.如果你想区分 class(医生,用户),你需要在$doc (或者甚至像User::role集合这样的超集)上运行一个额外的命令,比如get_class但这将取决于你如何设置查询并向上流动。 I'm not sure there is a direct method in Spatie to do exactly how you wish, but if you are OK with pulling a superset collection of users which might include the Doctor class, you can do it in two steps at least.我不确定 Spatie 中是否有一种直接方法可以完全按照您的意愿进行操作,但是如果您可以提取用户的超集集合(其中可能包括 Doctor class),您至少可以分两步完成。

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

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