Laravel查询生成器高级AND,OR

时间:2014-01-24 03:04:36

标签: mysql select laravel where query-builder

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

它产生:

 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)

有什么想法吗?

2 个答案:

答案 0 :(得分: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);
    });

答案 1 :(得分:0)

快速方法。阅读原始表达式:

http://laravel.com/docs/queries#raw-expressions