Laravel 雄辩查询中的原始SQL

Laravel 雄辩查询中的原始SQL,laravel,laravel-4,eloquent,Laravel,Laravel 4,Eloquent,我有一个用户表、角色表和用户角色表 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-&

我有一个用户表、角色表和用户角色表

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.
我哪里出错了?

这应该行得通。 如果用户没有指定的角色,则查询应返回null

$userId = 1;
$role = array('admin', 'public');

$data = User::whereHas('roles', function($q) use ($role){

    $q->whereIn('title', $role);

}, '>=', count($role))->find($userId);