我有一个用户表,角色表和一个user_role表。
User
id | name
Roles
id | title
User_Role
user_id | role_id
我需要一个雄辩的查询来查找用户是否拥有所有角色。
到目前为止,我有:
$userId = 1;
$role = array('admin', 'public');
$data = User::whereHas('roles', function($q) use ($role){
$q->whereRaw($q->whereIn('title', $role)->count() .' = '.count($role));
})->find($userId);
但我收到错误:
Unknown column user.id in where clause.
我哪里错了?
答案 0 :(得分:1)
这应该有效。 如果用户没有指定的角色,则查询应返回null。
$userId = 1;
$role = array('admin', 'public');
$data = User::whereHas('roles', function($q) use ($role){
$q->whereIn('title', $role);
}, '>=', count($role))->find($userId);