In 拉拉维尔 5.3 https://laravel.com/docs/5.3/queries#where-clauses(并且截至目前仍然如此7.x https://laravel.com/docs/7.x/queries#where-clauses)您可以使用更细粒度的 wheres 作为数组传递:
$query->where([
['column_1', '=', 'value_1'],
['column_2', '<>', 'value_2'],
[COLUMN, OPERATOR, VALUE],
...
])
就我个人而言,我还没有找到超过多个的用例where
调用,但事实是您可以使用它。
自 2014 年 6 月起,您可以将数组传递给where
只要你想要所有的wheres
use and
运算符,您可以这样对它们进行分组:
$matchThese = ['field' => 'value', 'another_field' => 'another_value', ...];
// if you need another group of wheres as an alternative:
$orThose = ['yet_another_field' => 'yet_another_value', ...];
Then:
$results = User::where($matchThese)->get();
// with another group
$results = User::where($matchThese)
->orWhere($orThose)
->get();
上面将导致这样的查询:
SELECT * FROM users
WHERE (field = value AND another_field = another_value AND ...)
OR (yet_another_field = yet_another_value AND ...)