2015-03-30 15 views
7

ile birden çok where yan tümcelerini kullanma Aşağıdaki SQL sorgusu laravels sorgu oluşturucu ile çalışmak için çok fazla sorun yaşıyorum.laravel sorgu oluşturucusu

SELECT * FROM gifts 
JOIN giftcategory ON gifts.id = giftcategory.giftid 
JOIN giftoccasions ON gifts.id = giftoccasions.giftid 
JOIN giftrelationship ON gifts.id = giftrelationship.giftid 

WHERE (gifts.gender = 'any' OR gifts.gender = 'male') 
AND giftoccasions.occasionid = '2' 
AND (giftcategory.categoryid = '0' OR giftcategory.categoryid = '1') 
AND giftrelationship.relationshipid = '1' 

Bu sorgu çalışıyor, ancak Laravels sorgu oluşturucu kullanırken i aynı sonuçları elde edemezsiniz. Şimdiye kadar aşağıdaki kod var. Hiç düzgün çalışmıyor. Sorunun, ya daWhere bölümü ile yatar olabileceğini düşünüyorum çünkü bu, diğer herhangi bir ifadeyle uyuşmayan sonuçları döndürüyor gibi görünüyor.

$giftQuery = DB::Table('gifts') 
->Join('giftcategory', 'gifts.id', '=', 'giftcategory.giftid') 
->Join('giftoccasions', 'gifts.id', '=', 'giftoccasions.giftid') 
->where('gifts.gender', '=', "male") 
->orwhere('gifts.gender', '=', "any") 
->where('giftoccasions.occasionid', '=', "2") 
->where('giftoccasions.relationshipid', '=', "1") 
->Where('giftcategory.categoryid', '=', "0") 
->orWhere('giftcategory.categoryid', '=', "1"); 
+0

, bunu kabul edebilir :) –

cevap

12

Sen parametre gruplandırma ile advanced where kullanmak istiyorum: Aşağıda cevabım memnunsanız

$giftQuery = DB::table('gifts') 
    ->join('giftcategory', 'gifts.id', '=', 'giftcategory.giftid') 
    ->join('giftoccasions', 'gifts.id', '=', 'giftoccasions.giftid') 
    ->where(function($query) { 
     $query->where('gifts.gender', '=', "male") 
      ->orWhere('gifts.gender', '=', "any"); 
    }) 
    ->where('giftoccasions.occasionid', '=', "2") 
    ->where('giftoccasions.relationshipid', '=', "1") 
    ->where('giftcategory.categoryid', '=', "0") 
    ->orWhere('giftcategory.categoryid', '=', "1"); 
İlgili konular