2016-11-03 50 views
1

我试图把计数约束laravel雄辩的嵌套关系,但它没有按预期工作。计数约束不工作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(); 

这里它返回的客房,是在该日期范围avuable(即dateWisepricing IM空集)

任何请帮助

回答

0

最好使用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(); 

这种方法将查询出所有无法使用的记录。 hotelsroomsdate_wise_pricings表的名字,但其实我不知道你怎么称呼他们:

注意

,我使用求佛。

+0

dayWisePricing是在房间的关系不是酒店的 – kamalakar

+0

是的,我已经重新编辑我进入 –

+0

请你解释一下它是如何从我前面的代码不同,因为它返回相同的结果和约束,也没有工作,因为它 – kamalakar