[英]count constraint not working laravel eloquent nested relations
我试图将计数约束放在laravel雄辩的嵌套关系上,但它没有按预期工作。
这里的场景是:fetch酒店那些有日期范围的房间
$hotels = Hotel::where('destination_id', $destinationId) - > with(['rooms' = > function ($query) use($totalNights, $check_in, $check_out) { $query - > with([ 'dateWisePricing' = > function ($dateWisePricing) use($check_in, $check_out) { $dateWisePricing - > where('date', '>=', $check_in); $dateWisePricing - > where('date', '<', $check_out); $dateWisePricing - > orderBy('date'); } ]); $query - > has('dateWisePricing', '>=', $totalNights); } ]) - > has('rooms.dateWisePricing') - > get();
在这里,它将返回在日期范围内无法使用的房间(即dateWisepricing im empty collection)
请帮忙
最好使用whereHas
而不是仅查询with
方法。 但最好的选择是使用这样的连接查询它:
$hotels = Hotel::select('hotels.*')
->with(['rooms.dateWisePricing' => function ($dateWisePricing) {
$dateWisePricing - > orderBy('date');
}])
->join('rooms', 'rooms.hotel_id', '=', 'hotels.id')
->join('date_wise_pricings', 'date_wise_pricings.id', '=', 'rooms.date_wise_pricing_id')
->where('date_wise_pricings.date', '>=', $check_in)
->where('date_wise_pricings.date', '<', $check_out)
->where('destination_id', $destinationId)
->groupBy('hotels.id')
->get();
这种方法将查询所有不可用的记录。
注意
我使用的提示: hotels
, rooms
和date_wise_pricings
表名,但事实上我不知道你怎么称呼他们。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.