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

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

Can I do Model->where('id', ARRAY) multiple where conditions?

phplaravellaravel-4laravel-5where

提问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

There's whereIn():

whereIn()

$items = DB::table('items')->whereIn('id', [1, 2, 3])->get();

回答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 whereInwhich accepts an array as second paramter.

您可以使用whereInwhich 接受一个数组作为第二个参数。

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 ANDoperator. if you need ORyou can use orWheremethod.

这将使用AND运算符。如果你需要OR你可以使用orWhere方法。

For advanced wherestatements

对于高级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();