php 我可以做 Model->where('id', ARRAY) 多个 where 条件吗?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/30706603/
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
Can I do Model->where('id', ARRAY) multiple where conditions?
提问by Vudew
The title says it all.
标题说明了一切。
I get that I can do this :
我知道我可以做到这一点:
DB::table('items')->where('something', 'value')->get()
But what I want to check the where condition for multiple values like so:
但是我想检查多个值的 where 条件,如下所示:
DB::table('items')->where('something', 'array_of_value')->get()
Is there an easy way of doing this?
有没有简单的方法来做到这一点?
回答by Limon Monte
回答by behzad babaei
You could use one of the below solutions:
您可以使用以下解决方案之一:
$items = Item::whereIn('id', [1,2,..])->get();
or:
或者:
$items = DB::table('items')->whereIn('id',[1,2,..])->get();
回答by Gediminas
If you need by several params:
如果您需要多个参数:
$ids = [1,2,3,4];
$not_ids = [5,6,7,8];
DB::table('table')->whereIn('id', $ids)
->whereNotIn('id', $not_ids)
->where('status', 1)
->get();
回答by chanafdo
You can use whereIn
which accepts an array as second paramter.
您可以使用whereIn
which 接受一个数组作为第二个参数。
DB:table('table')
->whereIn('column', [value, value, value])
->get()
You can chain where multiple times.
您可以多次链接 where。
DB:table('table')->where('column', 'operator', 'value')
->where('column', 'operator', 'value')
->where('column', 'operator', 'value')
->get();
This will use AND
operator. if you need OR
you can use orWhere
method.
这将使用AND
运算符。如果你需要OR
你可以使用orWhere
方法。
For advanced where
statements
对于高级where
语句
DB::table('table')
->where('column', 'operator', 'value')
->orWhere(function($query)
{
$query->where('column', 'operator', 'value')
->where('column', 'operator', 'value');
})
->get();
回答by Arun
WHERE AND SELECT Condition In Array Format Laravel
Laravel 数组格式中的 WHERE AND SELECT 条件
use DB;
$conditions = array(
array('email', '=', '[email protected]')
);
$selected = array('id','name','email','mobile','created');
$result = DB::table('users')->select($selected)->where($conditions)->get();