簡體   English   中英

如何通過雄辯地與Entrust一起使用Role ='admin'角色的所有用戶?

[英]How do I get all Users with Roles where role = 'admin' using eloquent along with Entrust?

我正在使用Laravel Entrust軟件包https://github.com/Zizaco/entrust我想讓所有用戶都具有這樣的角色

name | role

Ryan | admin
Megan | admin

表結構

users 
id,name,email,password

roles, 
id,name

role_user (pivot table)
id,user_id

我試過了但是沒用

$users = User::with('roles')->where('roles.name','=','admin')->get();

錯誤

Column not found: 1054 Unknown column 'roles.name' in 'where clause' (SQL: select * from users where roles.name = admin)

我既不想使用RAW查詢,也不想使用

$users = DB::table('users')->select('users.name as username', 'role.name as role')->with('roles')->join('roles', 'roles.user_id', '=', 'users.id')->where('roles.name', 'admin')->get();

還有其他辦法嗎?

whereHas使用

$users = User::whereHas('roles', function($q)
{
    $q->where('name', 'admin');
})->get();

使用它,它將檢索具有管理員角色的所有用戶

$users = User::with(['roles' => function($query)
{
    $query->where('name','=','admin');
}])->get();

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM