【问题标题】:How to apply 'And' condition inside an 'OR' condition in Laravel elequont如何在 Laravel eloquent 的“或”条件中应用“与”条件
【发布时间】:2014-06-22 16:11:08
【问题描述】:

我的查询是这样的

SELECT * FROM activities WHERE (active=1 AND linked_project_id IN (2,3) AND GLOBAL=1 OR (linked_project_id=2  AND active=1))

但我不知道如何将 'linked_project_id=2 AND active=1' 放入 AND 与 Laravel。 这是我的代码-

$activities=  Activitie::where('active','=',1)->whereIn('linked_project_id',array(2,3))->where('global','=','1')->orWhere('linked_project_id','='2)->where('active','=',1)->get();

但这会产生类似

的结果
SELECT * FROM activities WHERE (active=1 AND linked_project_id IN (2,3) AND GLOBAL=1 OR (linked_project_id=2 ) AND active=1)

【问题讨论】:

  • 能否举个例子

标签: php sql laravel eloquent where-clause


【解决方案1】:

使用Advanced Wheres

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

【讨论】:

    猜你喜欢
    • 2016-12-29
    • 2022-07-06
    • 2015-05-20
    • 2019-12-28
    • 2018-05-24
    • 2017-03-21
    • 1970-01-01
    • 2014-11-15
    • 1970-01-01
    相关资源
    最近更新 更多