mysql - Laravel 在 whereHas 中的多个 where 子句约束 hasMany 关系
问题描述
我有以下查询...
$pgcs = PrivateGuard::with(['licences', 'state'])
->whereHas('licences', function($query){
$query->whereDate('expiration_of_licence', '<', Carbon::today())
->where('renewal', 0);
})
->where('status', 1)
->get();
我想获得具有满足上述条件的许可证的 $pgcs, whereDate 可以正常工作,但是 where('renewal', 0) 似乎不能正常工作。上面的查询得到 $pgcs 的许可证也有 = 1 的续订,虽然相同的 $pgc 也有续订值为 0 的许可证和另一个具有 1 的许可证,但我不想要一个具有任何续订价值的许可证的 $pgc 1 要在此查询中检索。我该怎么办?
解决方案
你的情况是
- [1] pgc .. 有两个或多个续订 0 , 1 的许可证
- [2] pgc .. 有两个或多个续订 0 , 0 的许可证
- [3] pgc .. 有两个或更多许可证续约 1 , 1
现在您的查询正在获取案例 [1]、[2] 的 pgcs
所以你必须稍微改变你的查询逻辑,才能得到 [2]
$pgcs = PrivateGuard::with(['licences', 'state'])
->whereHas('licences', function($query){
$query->whereDate('expiration_of_licence', '<', Carbon::today())
->where('renewal', 0);
})
->whereDoesntHave('licences', function($query){
$query->where('renewal', 1);
})
->where('status', 1)
->get();
推荐阅读
- google-cast - 是否可以使用 Google Cast API 在 google home/cast 设备中返回之前的投射会话
- sockets - 后端服务通信时如何区分 TCP 连接?
- canvas - ThreeJS - 沿样条线移动相机,同时保持轨道控制平移
- java - 这个 Java 布尔表达式有什么问题?为什么它会做它做的事情?
- java - 如何在字符串中的字符左右添加空格?
- r - 如何在 tidycensus 包中获取州内每个县的 zcta 代码列表?
- python - 我将如何修复此 pkg_resources.DistributionNotFound:未找到 'pygmt' 发行版,应用程序需要该发行版
- java - 在 android studio 中使用 TextChangeHandler 计算
- python - 是否有修复脚本中混合列类型的 DtypeWarning 的方法?
- java - 如何解决a.io.InvalidClassException:org.ejml.data.DMatrixRMaj;本地类不兼容:流 classdesc serialVersionUID