2015-03-30 12 views
7

Im avendo molti problemi a convertire la seguente query SQL per lavorare con il generatore di query di laravels.Utilizzo di più clausole where con il generatore di query laravel

Questa query funziona correttamente, ma non posso ottenere gli stessi risultati quando si utilizza il generatore di query di Laravels. Ho il seguente codice finora. Non funziona affatto correttamente. Sto pensando che il problema potrebbe riguardare la parte o Where perché sembra che stiano restituendo risultati che non corrispondono a nessuna delle altre clausole.

$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

se sei felice con la mia risposta qui sotto, si può accettare :) –

risposta

12

che si desidera utilizzare con il parametro advanced where raggruppamento:

$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"); 
Problemi correlati