2016-11-03 5 views
1

laravel擬似ネストされたリレーションにcount制約を設定しようとしていますが、期待通りに機能しません。カウント制約が動作していないlaravel擬似ネスト関係

ここでシナリオは次のとおりです。これらは、ここでは、任意の(すなわちdateWisepricingイム空のコレクション)

を日付範囲でavuableていない部屋を返すの日付範囲で

 
    $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(); 

利用可能なお部屋を持っているホテルをフェッチ助けてください

答えて

0

withメソッドを照会するのではなく、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(); 

この方法では、利用できないすべてのレコードを照会します。

注意私が使用

Metion:hotelsroomsdate_wise_pricingsテーブル名を実際に私はあなたがそれらを呼び出す方法を知ってはいけません。

+0

dayWisePricingは、あなたが、それは同じ結果を返すだとして、それは、私の以前のコードとは異なるのですか説明してくださいだろうと、それはまあ – kamalakar

+0

の部屋ではないの関係であります'with'の内部で酔ってしまうと、内部関係(ホテルではない)の結果だけを制約することができます。ジョインを使用すると、RELATIONのいずれかのリゾルトが存在しないすべてのレコードを照会できます。これが '$ check_in'と' $ check_out'varsの日付形式をchackeよりもうまく動作しない場合、 –

+0

を再編集しましたホテル – kamalakar

関連する問題