php - 使用 whereIn 从数据透视表返回数据
问题描述
所以我有Status class
它与数据透视表的关系roles
:
public function roles():
{
return $this->belongsToMany(Role::class, 'status_role', 'status_id', 'role_id');
}
这是Status
db 表的外观:
id title
1 status1
2 status2
3 status3
然后我的数据透视表如下所示:
status_id role_id
1 2
2 2
现在我想query
用role_id=2
.
基本上它应该返回这样的数据:status1, status2
而不是包含status3
.
我尝试过的:
$statuses = Status::query()
->leftJoin('status_role', function ($join) {
$join->on('statuses.id', '=', 'status_role.status_id')
->whereIn('status_role.role_id',[2]);
})
->get();
但现在它返回所有状态(status1、status2、status3)它应该只是(status1 和 status2)。我需要如何改变它?
解决方案
此查询将返回所有附加到 id 为 2 的角色的状态:
Status::query()->whereHas('roles', function($q){
$q->where('id', 2);
})->get();
它使用whereHas
在您需要查询关系时可能有用的方法。
它可以做得更多,您应该查看有关此主题的文档:https ://laravel.com/docs/8.x/eloquent-relationships#querying-relationship-existence
快速说明:whereHas
是做你想要实现的目标的“Laravel 首选方式”。
但是,您应该也可以使用此查询来执行此操作,它更接近您当前的代码:
$statuses = Status::query()
->join('status_role', function ($join) {
$join
->on('statuses.id', '=', 'status_role.status_id')
->where('status_role.role_id',2);
})
->get();
// I replaced the leftJoin by join, which will exclude all results without roles (e.g. status id 3)
// or even simpler:
$statuses = Status::query()
->join('status_role', 'statuses.id', '=', 'status_role.status_id')
->where('status_role.role_id',2)
->get();
推荐阅读
- r - 按组以字符格式汇总金额
- docker - 版本 3.28.1 中缺少 Nexus `admin.password` 文件
- android - 通过大轮廓 TextInputLayout 中的填充剪切的文本
- ios - 如果不在视图中,则在提交期间 Tableview 文本输入字段为零
- php - 为什么 AND SQL 运算符会使查询失败?
- html - 我想改变我的图像和文本之间的距离,并将所有项目向左移动,就像图二一样
- java - 如何从 2 个存储 x,y 值的 Arraylists 创建动画,并给出整数索引之间的时间变化?
- mysql - 在插入触发器之前在 MYSQL 中使用 IF 语句删除“The”
- spring-boot - 希望资源在 Spring WebFlux 中使用 Spring Security 实现身份验证
- php - 在 PHP 中验证 ID 令牌时出错 - Google API