Laravel 查询:where 子句中有多个条件,但 1 个条件需要比较运算符
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/35531456/
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
Laravel query: multiple conditions in where clause but 1 condition requires comparison operator
提问by The One and Only ChemistryBlob
I have been trying to get a Laravel select query to work which requires multiple AND conditions. I understand you could normally do this:
我一直在尝试让 Laravel 选择查询工作,这需要多个 AND 条件。我知道你通常可以这样做:
$myArray = array('column_one' => 1, 'column_two' => 2);
DB::('mytable')->select('mycolumn')->where($myArray)->get();
How do you do this with one where statement when a comparison operator is required?
当需要比较运算符时,你如何用一个 where 语句来做到这一点?
Something like:
就像是:
DB::('mytable')->select('mycolumn')->where('column_one', 1)->where('column_two' '<', 10000)->get();
but with only one where statement instead of chaining them. I searched the Laravel docs and could not find this.
但只有一个 where 语句而不是链接它们。我搜索了 Laravel 文档,但找不到这个。
Any help is appreciated!
任何帮助表示赞赏!
回答by lagbox
If you are on 5.2 and a up to date version of it you can pass an array of arrays with the operator in them.
如果您使用的是 5.2 及其最新版本,则可以传递带有运算符的数组数组。
From the 5.2 docs:
来自 5.2 文档:
$users = DB::table('users')->where([
['status','1'],
['subscribed','<>','1'],
])->get();
If you have a operator you want to use, it would be the second element of the array which has 3 elements. In the example above ['subscribed', '<>', '1']
, the '<>'
is the operator.
如果您有一个要使用的运算符,它将是具有 3 个元素的数组的第二个元素。在上面的例子中['subscribed', '<>', '1']
,'<>'
是运算符。