php Laravel 4 Eloquent Query 使用 WHERE 和 OR AND OR?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/16995102/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-25 12:02:34  来源:igfitidea点击:

Laravel 4 Eloquent Query Using WHERE with OR AND OR?

phplaraveleloquentlaravel-query-builder

提问by Farzher

How do I say WHERE (a = 1 OR b =1 ) AND (c = 1 OR d = 1)

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

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

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

回答by rmobis

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

利用参数分组Laravel 4.2)。对于您的示例,它会是这样的:

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

回答by Yilmazerhakan

If you want to use parameters for a,b,c,d in Laravel 4

如果你想在 Laravel 4 中使用 a,b,c,d 的参数

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

回答by Samrong Prey Kabbas

if you want to use parentheses in laravel 4 and don't forget return
In Laravel 4 (at least) you need to use $a, $b in parentheses as in the example

如果你想在 Laravel 4 中使用括号并且不要忘记 return
在 Laravel 4 中(至少)你需要使用 $a, $b 在括号中,如示例中所示

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

This is my result: enter image description here

这是我的结果: 在此处输入图片说明

回答by Sabrina Leggett

Incase you're looping the OR conditions, you don't need the the second $query->where from the other posts (actually I don't think you need in general, you can just use orWhere in the nested where if easier)

如果您正在循环 OR 条件,则不需要第二个 $query->where 来自其他帖子(实际上我认为您通常不需要,如果更容易,您可以在嵌套的 where 中使用 orWhere)

$attributes = ['first'=>'a','second'=>'b'];

$query->where(function ($query) use ($attributes) 
{
    foreach ($attributes as $key=>value)
    {
        //you can use orWhere the first time, doesn't need to be ->where
        $query->orWhere($key,$value);
    }
});

回答by Majbah Habib

$a, $b, $c, $d can be dynamic values by the query

$a, $b, $c, $d 可以是查询的动态值

 ->where(function($query) use ($a, $b)
        {
            $query->where('a', $a)
                  ->orWhere('b',$b);
        })
 ->where(function($query) use ($c, $d)
        {
            $query->where('c', $c)
                  ->orWhere('d',$d);
        })

回答by srmilon

Simply Use in Laravel Eloquent:

只需在 Laravel Eloquent 中使用:

$a='foo', $b='bar', $c='john', $d='doe';

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

Will produce a query like:

将产生如下查询:

SELECT * FROM <table> WHERE (a='foo' or b='bar') AND (c='john' or d='doe');

回答by Krishnamoorthy Acharya

Another way without using Modal

不使用 Modal 的另一种方式

Database: stocks Columns:id,name,company_name,exchange_name,status enter image description here

数据库:股票 列:id,name,company_name,exchange_name,status 在此处输入图片说明

  $name ='aa'
  $stocks = DB::table('stocks')
            ->select('name', 'company_name', 'exchange_name')
            ->where(function($query) use ($name) {
                $query->where('name', 'like', '%' . $name . '%')
                ->orWhere('company_name', 'like', '%' . $name . '%');
            })
            ->Where('status', '=', 1)
            ->limit(20)
            ->get();

回答by Leon

You can also use query scopes to make things a bit tidier, so you can do something like:

您还可以使用查询范围使事情更整洁,因此您可以执行以下操作:

Invoice::where('account', 27)->notPaidAt($date)->get();

Then in your model

然后在你的模型中

public function scopeNotPaidAt($query, $asAt)
{
    $query = $query->where(function ($query) use ($asAt) { 
        $query->where('paid', '=', '0000-00-00')->orWhere('paid', '>=', $asAt); 
    });
    return $query;    
}

回答by ashok

You can also query the first or condition and later you can apply another or condition

您还可以查询第一个或条件,然后再应用另一个或条件

$model = Model::where('a',1)->orWhere('b',1);

now apply another condition on that $modelvariable

现在对该$model变量应用另一个条件

$model1 = $model->where('c',1)->orWhere('d',1)->get();

回答by parastoo amini

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