2014-01-24 68 views
1
$query = new TableA; 
$query = $query->join('tableB', 'tableA.b_id', '=', 'tableB.id'); 
$query = $query->where('tableB.something', '=', '1'); 
$query = $query->where('tableA.something_else', '=', '2'); 

它產生:Laravel查詢Builder高級何在AND,OR

WHERE something=1 AND something_else=2 AND .. AND.... 

,但現在 - 我需要這樣的:

WHERE something=1 AND something_else=2 AND (city=city1 OR city=city2 OR city=city3) AND (country=country1 OR country=country2) 

任何想法?

+0

不完全與你的問題有關,但你應該看看laravel的關係。它會爲你節省很多代碼,並使它看起來更好。 – elliotanderson

回答

2

利用Advanced Wheres。對於你的例子,它會是這樣的:

->where(function ($query) { 
     $query->where('city', '=', city1) 
      ->orWhere('city', '=', city2) 
      ->orWhere('city', '=', city3); 
    })->where(function ($query) { 
     $query->where('country', '=', country1) 
      ->orWhere('country', '=', country2); 
    });