Laravel Eloquent Query 使用 WHERE 和 OR AND OR?

我怎么说WHERE (a = 1 OR b =1 ) AND (c = 1 OR d = 1)

对于更复杂的查询,我应该使用原始 SQL 吗?

For more complicated queries am I supposed to use raw SQL?

推荐答案

利用 逻辑分组(Laravel 7.x/4.2).对于您的示例,它会是这样的:

Make use of Logical Grouping (Laravel 7.x/4.2). For your example, it'd be something like this:

Model::where(function ($query) {
    $query->where('a', '=', 1)
          ->orWhere('b', '=', 1);
})->where(function ($query) {
    $query->where('c', '=', 1)
          ->orWhere('d', '=', 1);
});

相关文章