[英]Laravel wherenotexists returning null
我有以下一段代码
$not_paid = Tenant::where('property_id', $property_id)
->whereNotExists(function ($query) use ($property_id) {
$query->select('user_id')
->from('rent_paids');
})
->get();
这应该是获取某个属性中的所有租户,在rent_paids
表中查找它们并返回不在rent_paids
表中的用户,如下所示:
租户表
ID | 用户身份 | property_id |
---|---|---|
1 | 1 | 1 |
2 | 2 | 1 |
3 | 3 | 1 |
租金支付
ID | 用户身份 | property_id | 支付的金额 |
---|---|---|---|
1 | 1 | 1 | 3000 |
我希望能够返回租户表中的用户,而不是rent_paids 表中的用户。 在这种情况下,用户2 and 3
。 但是上面的代码返回一个空数组。
您缺少将其绑定回原始表的 where 子句。
$not_paid = Tenant::where('property_id', $property_id)
->whereNotExists(function ($query) use ($property_id) {
$query->select('user_id')
->from('rent_paids')
->whereColumn('tenants.user_id', '=', 'rent_paids.user_id');
})
->get();
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.