whereHas中的Laravel多个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,其中Date正确工作,但其中('newal',0(似乎不正确工作。上面的查询得到的$pgc的许可证更新值也为=1,尽管同样的$pgc也有更新值为0的许可证和另一个更新值为1的许可证,但我不希望在这个查询中检索到更新值为%1的任何许可证。我该怎么办?

您的案例是

  • [1]页。。具有两个或多个续订为0的许可证,1
  • [2] pgc。。具有两个或多个续订为0的许可证,0
  • [3] pgc。。有两个或多个许可证,续期1,1

现在您的查询正在获取案例[1]、[2]的pgc

所以你必须稍微改变一下你的查询逻辑,只得到[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();

最新更新