Php 计数约束不起作用laravel雄辩的嵌套关系

Php 计数约束不起作用laravel雄辩的嵌套关系,php,laravel,eloquent,Php,Laravel,Eloquent,我试图对laravel雄辩的嵌套关系设置计数约束,但它并没有按预期工作 这里的场景是:取那些有日期范围内可用房间的酒店 $hotels = Hotel::where('destination_id', $destinationId) - > with(['rooms' = > function ($query) use($totalNights, $check_in, $check_out) { $query - > with([

我试图对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 - > where('date', '>=', $check_in); $dateWisePricing - > where('date', '<', $check_out); $dateWisePricing - > orderBy('date'); } ]); $query - > has('dateWisePricing', '>=', $totalNights); } ]) - > has('rooms.dateWisePricing') - > get(); $hotels=Hotel::where($destination\u id',$destinationId)->具有(['rooms'=>功能($query)使用($totalNights,$check\u in,$check\u out){ $query->with([ “dateWisePricing”=>函数($dateWisePricing)使用($check\u in,$check\u 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
,而不是仅使用方法查询
。但最好的选择是使用如下连接查询:

$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=Hotel::select('hotels.*')
->使用(['rooms.dateWisePricing'=>函数($dateWisePricing){
$dateWisePricing->orderBy('date');
}])
->加入('rooms'、'rooms.hotel_id'、'='、'hotels.id')
->join('date_-wise_-pricings'、'date_-wise_-pricings.id'、'='、'rooms.date_-wise_-pricings.id')
->其中('date\u wise\u pricings.date','>=',$check\u in)

->where('date\u wise\u pricings.date',”最好使用
whereHas
,而不是仅使用
方法查询
。但最好的选择是使用如下连接查询:

$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=Hotel::select('hotels.*')
->使用(['rooms.dateWisePricing'=>函数($dateWisePricing){
$dateWisePricing->orderBy('date');
}])
->加入('rooms'、'rooms.hotel_id'、'='、'hotels.id')
->join('date_-wise_-pricings'、'date_-wise_-pricings.id'、'='、'rooms.date_-wise_-pricings.id')
->其中('date\u wise\u pricings.date','>=',$check\u in)

->where('date\u wise\u pricings.date','daywise pricings是房间上的关系,而不是酒店上的关系。请您解释一下它与我以前的代码有什么不同,因为它返回了相同的结果,并且约束也不再像以前那样起作用了。如果您在
内使用
那么您只能约束内部关系的结果(不是酒店)。如果使用联接,则可以查询出没有任何关系结果的所有记录。如果此操作不起作用,请更改
$check\u in
$check\u out
vars的日期格式。总之,我没有看到联接查询语句的使用,但一旦我将check\u in&check\u out约束放入roos.dateWisepricing中,然后它会像以前一样返回结果,不做任何更改。我的意思是说,在两个日期之间不可用的房间仍然会出现在结果集中。一个疑问是,在给定日期内可用的房间的约束条件在哪里?价格是房间上的关系,而不是酒店上的关系。请解释一下它与我以前的同事有什么不同de,因为它返回相同的结果,并且约束也不像以前那样起作用。如果您使用
中定义,那么您只能约束内部关系的结果(而不是酒店)。如果使用联接,则可以查询出没有任何关系结果的所有记录。如果此操作不起作用,请更改
$check\u in
$check\u out
vars的日期格式。总之,我没有看到联接查询语句的使用,但一旦我将check\u in&check\u out约束放入roos.dateWisepricing中,然后,它会像以前一样返回结果,没有任何更改。我的意思是说,在两个日期之间不可用的房间仍然会出现在结果集中。一个疑问是,在给定日期内可用的房间的约束条件在哪里