Mysql 获取多对多关系中除管理员以外的所有用户
我有一个users表和roles表,在role\u user表中以多对多关系将它们连接起来。 我想获得除具有管理员角色的用户之外的所有用户,我想包括没有任何角色的用户。 基本上,除了管理员之外,所有用户都是。Mysql 获取多对多关系中除管理员以外的所有用户,mysql,laravel,many-to-many,Mysql,Laravel,Many To Many,我有一个users表和roles表,在role\u user表中以多对多关系将它们连接起来。 我想获得除具有管理员角色的用户之外的所有用户,我想包括没有任何角色的用户。 基本上,除了管理员之外,所有用户都是。 在用户模型中添加关系 User.php 检索 对于普通MYSQL 在用户模型中添加关系 User.php 检索 对于普通MYSQL 你能试试这个吗。如果您不使用模型关系,这将起作用 $users = DB::table(role_user as ru') -
在用户模型中添加关系 User.php 检索 对于普通MYSQL
在用户模型中添加关系 User.php 检索 对于普通MYSQL
你能试试这个吗。如果您不使用模型关系,这将起作用
$users = DB::table(role_user as ru')
->join('users as u', 'ru.user_id', '=', 'u.id')
->join('roles as r', 'r.id', '=', 'ru.id')
->where('r.name', '<>', 'admin')->get()
你能试试这个吗。如果您不使用模型关系,这将起作用
$users = DB::table(role_user as ru')
->join('users as u', 'ru.user_id', '=', 'u.id')
->join('roles as r', 'r.id', '=', 'ru.id')
->where('r.name', '<>', 'admin')->get()
如果关系设置正确,则可以通过以下方式轻松实现: 关于注释:如果要检索至少具有一个角色的所有用户,但其角色可能不包含管理员角色,则可以使用以下查询:
$roleToExclude = 1;
$users = User::query()
->has('roles')
->whereDoesntHave('roles', function (Builder $query) use ($roleToExclude) {
$query->where('id', $roleToExclude);
})
->get();
has'roles'将确保用户存在一个角色,而where's'roles',fn将确保它不是管理员角色
关于@Jino Antony建议编辑的注释:
在处理多对多关系时,查询生成器的所有whereX$col$val方法在本例中操作其他表角色,而不是透视表角色\用户。要查询数据透视表上的列,在我的示例中需要使用wherePivot'role_id',$roleToExclude。如果关系设置正确,那么使用whereDoesntHave可以很容易地实现这一点: 关于注释:如果要检索至少具有一个角色的所有用户,但其角色可能不包含管理员角色,则可以使用以下查询:
$roleToExclude = 1;
$users = User::query()
->has('roles')
->whereDoesntHave('roles', function (Builder $query) use ($roleToExclude) {
$query->where('id', $roleToExclude);
})
->get();
has'roles'将确保用户存在一个角色,而where's'roles',fn将确保它不是管理员角色
关于@Jino Antony建议编辑的注释:
在处理多对多关系时,查询生成器的所有whereX$col$val方法在本例中操作其他表角色,而不是透视表角色\用户。要查询数据透视表上的列,在我的示例中需要使用wherePivot'role_id',$roleToExclude。尝试以下代码:
$users = User::whereDoesntHave('roles', function ($query) {
$query->where('name', Role::ROLE_ADMIN);
})->get();
在User.php文件中添加关系代码
public function roles()
{
return $this->belongsToMany(Role::class, 'role_user', 'user_id', 'role_id');
}
请尝试以下代码:
$users = User::whereDoesntHave('roles', function ($query) {
$query->where('name', Role::ROLE_ADMIN);
})->get();
在User.php文件中添加关系代码
public function roles()
{
return $this->belongsToMany(Role::class, 'role_user', 'user_id', 'role_id');
}
由于上述答案缺少相反的方法,我添加了这种关系
public function roles()
{
return $this->belongsToMany(Role::class);
}
//roles that need to be excuded
//it also accepts the array
$rolesExcept = 'admin';
//roles that need to be included
//it also accepts the array
$rolesOnly = 'admin';
//closure that filter the the rolesOnly
$withSpecificRolesClosure = function ($query) use ( $rolesOnly)
{
$query-> whereIn( 'name', (array) $rolesOnly); // role with only admin
};
//closure that filter the the rolesExcept
$withOutSpecificRolesClosure = function ($query) use ( $rolesExcept)
{
$query->whereNotIn('name', (array)$rolesExcept); // role with no admin
};
//get all the users with the role with admim
$userWithRoleAdmin = App\Models\User::whereHas('roles', $withSpecificRolesClosure)->get();
//get all the users with the role without admim
$userWithOutRoleAdmin = App\Models\User::whereHas('roles',$withOutSpecificRolesClosure)->get();
由于上述答案缺少相反的方法,我添加了这种关系
public function roles()
{
return $this->belongsToMany(Role::class);
}
//roles that need to be excuded
//it also accepts the array
$rolesExcept = 'admin';
//roles that need to be included
//it also accepts the array
$rolesOnly = 'admin';
//closure that filter the the rolesOnly
$withSpecificRolesClosure = function ($query) use ( $rolesOnly)
{
$query-> whereIn( 'name', (array) $rolesOnly); // role with only admin
};
//closure that filter the the rolesExcept
$withOutSpecificRolesClosure = function ($query) use ( $rolesExcept)
{
$query->whereNotIn('name', (array)$rolesExcept); // role with no admin
};
//get all the users with the role with admim
$userWithRoleAdmin = App\Models\User::whereHas('roles', $withSpecificRolesClosure)->get();
//get all the users with the role without admim
$userWithOutRoleAdmin = App\Models\User::whereHas('roles',$withOutSpecificRolesClosure)->get();
你需要一个有说服力的查询吗?你需要一个有说服力的查询吗?这个查询至少有一个角色不同于管理员角色的用户。它还将查询具有其他角色的管理员。我不知道这一点。感谢您至少有一个角色不同于管理员角色的用户查询。它还将查询具有其他角色的管理员。我不知道这一点。谢谢,是的。这是一个更好的解决方案。我找到了如何使用SQL语句解决它,但我需要帮助将其转换为laravel方式选择users.name从不存在的用户中选择user\u id从role\u user WHERE users.id=role\u user.user\u id和role\u id=2或role\u id=3 ORDER BY users.id;这是我建议的更复杂的查询,如果ID为1、2和3的角色正好有3个,其中1是“admin”角色。使用此查询,无论何时添加新角色,您都必须调整WHERE子句—对于我的版本,您不需要这样做。但公平地说,我的查询也将返回没有任何角色的用户-您的查询将返回至少有一个id为2或3的角色的用户。不过,这可以更容易地实现。请参阅我的编辑。感谢使用$users=User::whereDoesntHave'roles',函数$query{$query->where'name','admin'->或where'name','root';}->get;对这是一个更好的解决方案。我找到了如何使用SQL语句解决它,但我需要帮助将其转换为laravel方式选择users.name从不存在的用户中选择user\u id从role\u user WHERE users.id=role\u user.user\u id和role\u id=2或role\u id=3 ORDER BY users.id;这是我建议的更复杂的查询,如果ID为1、2和3的角色正好有3个,其中1是“admin”角色。使用此查询,无论何时添加新角色,您都必须调整WHERE子句—对于我的版本,您不需要这样做。但公平地说,我的查询也将返回没有任何角色的用户-您的查询将返回至少有一个id为2或3的角色的用户。不过,这可以更容易地实现。请参阅我的编辑。感谢使用$users=User::whereDoesntHave'roles',函数$query{$query->where'name','admin'->或where'name','root';}->get;