【问题标题】:Laravel 4 eloquent WHERE with AND and OR?Laravel 4 雄辩的 WHERE 与 AND 和 OR?
【发布时间】:2015-06-02 10:24:49
【问题描述】:

WHERE (a=x AND b=y)(c=z AND d=j) 怎么说

对于 x , y , z .. 是变量。

如何用 eloquent 做到这一点?

【问题讨论】:

    标签: laravel-4 eloquent


    【解决方案1】:

    对于多个 where 语句使用:

    $result = Model::whereRaw('(a = ? and b=?) or (c=? and d=?)', ['x','y','z','j'])->get();
    

    【讨论】:

      【解决方案2】:

      您可以通过将闭包传递给查询构建器的where() 函数来实现此目的。这在文档 http://laravel.com/docs/5.0/queries#advanced-wheres

      的高级 wheres 部分中有介绍
      $results = MyModel::where(function($query) {
              $query->where('a', 'x')
                    ->where('b', 'y');
          })->orWhere(function($query) {
              $query->where('c', 'z')
                    ->where('d', 'j');
          })->get();
      

      【讨论】:

      猜你喜欢
      • 2016-07-05
      • 2014-08-23
      • 2017-01-30
      • 1970-01-01
      • 1970-01-01
      • 2021-07-04
      • 1970-01-01
      • 2014-01-03
      • 2018-09-18
      相关资源
      最近更新 更多