laravel - Laravel BelongsToMany 与 WhereHas 条件的关系
问题描述
我的数据库中有一些关系:
public function managers()
{
return $this->belongsToMany(User::class, 'manager_location', 'location_id', 'manager_id')
}
这种关系运作良好 - 返回与某个位置同步的所有用户。我正在使用spatie/laravel-permissions
,我的用户有不同的角色。当我尝试返回具有某些角色的用户(添加whereHas
到关系中)时,它会继续返回所有记录:
public function managers()
{
return $this->belongsToMany(User::class, 'manager_location', 'location_id', 'manager_id')->whereHas('roles', function ($q) {
$q->where('name', '=','manager');
});
}
我哪里出错了?
PS Mysql日志查询:
select * from `users` inner join `manager_location` on `users`.`id` = `manager_location`.`manager_id` where `manager_location`.`location_id` is null and exists (select * from `roles` inner join `model_has_roles` on `roles`.`id` = `model_has_roles`.`role_id` where `users`.`id` = `model_has_roles`.`model_id` and `model_has_roles`.`model_type` = ? and `name` = ?)
QueryBuilder 转储:https ://pastebin.com/j6ik35mK
解决方案
推荐阅读
- python - 将字符串拆分为子字符串
- node.js - 将 Content-Type 设置为 json 但请求以 x-www-form-urlencoded 形式出现
- python - 无法使用 partitionByInstrument 将文件分成几部分
- node.js - 在 Windows 64 位系统上,'ng' 未被识别为内部或外部命令
- java - Eclipse 安装失败并出现错误
- reactjs - 如何减少生产中的 reactjs 文件大小?
- ios - 金属着色器在缓冲区中插入值
- android - 更改对象/类
- string - Shell.Application getDetailsOf jpg Exif DateTaken 无效 DateTime
- ios - 避免在应用程序处于前台时显示通知警报