我想弄清楚是否有一种简单的方法来获取所有具有角色或其他角色的用户。
在 Spatie Laravel Permissions 的官方文档中,我找不到这个。
我正在尝试这样,但我得到了所有用户。
User::role(['Individual', 'Organisation', 'Venue'])->get();
我也在这样尝试:
User::whereHas("roles", function($q) {
$q->orWhere(function ($query) {
$query->where("name", "Individual")
->where("name", "Venue")
->where("name", "Organisation");
});
})->get();
在 Db 角色表中我有:
最佳答案
你可以试试whereIn()
到roles()引用链接 https://laravel.com/docs/8.x/queries#additional-where-clauses
User::with("roles")->whereHas("roles", function($q) {
$q->whereIn("name", ["Individual","Venue",'Organisation']);
})->get();
尝试使用 id
进行更好的搜索,因为它是主键
User::with("roles")->whereHas("roles", function($q) {
$q->whereIn("id", [6,7,8]);
})->get();
NOTE here
name
should match exact string of roles name which in your database
关于laravel - Spatie Laravel 权限 - 如何获取具有一个角色或另一个角色的用户,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/64658911/