我试图对laravel雄辩嵌套关系施加计数约束,但它不像预期的那样工作。
这里的场景是:获取在日期范围内有可用房间的酒店
<>之前$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 ->("约会的地方 ', '>=', $ check_in);$dateWisePricing -> where('date', '<, $check_out);$dateWisePricing -> order ('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
表名,但实际上我不知道你怎么称呼它们。