php Laravel whereIn 或 whereIn

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/22758819/
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 16:13:56  来源:igfitidea点击:

Laravel whereIn OR whereIn

phpmysqllaravellaravel-4query-builder

提问by Laky

I'm making a products search by filters:

我正在按过滤器进行产品搜索:

My code:

我的代码:

->where(function($query) use($filter)
{
  if(!empty($filter)){
    foreach ($filter as $key => $value) {           
      $f = explode(",", $value);        
      $query-> whereIn('products.value', $f);
    }           
  }
})

Query:

询问:

and (products.value in (Bomann, PHILIPS) and products.value in (red,white)) 

But I need:

但是我需要:

and (products.value in (Bomann, PHILIPS) OR products.value in (red,white)) 

Is there something similar in Laravel:

Laravel 中是否有类似的东西:

orWhereIn

回答by Vit Kos

You could have searched just for whereInfunction in the core to see that. Here you are. This must answer all your questions

您可以只搜索whereIn核心中的函数来查看它。这个给你。这必须回答你所有的问题

/**
 * Add a "where in" clause to the query.
 *
 * @param  string  $column
 * @param  mixed   $values
 * @param  string  $boolean
 * @param  bool    $not
 * @return \Illuminate\Database\Query\Builder|static
 */
public function whereIn($column, $values, $boolean = 'and', $not = false)
{
    $type = $not ? 'NotIn' : 'In';

    // If the value of the where in clause is actually a Closure, we will assume that
    // the developer is using a full sub-select for this "in" statement, and will
    // execute those Closures, then we can re-construct the entire sub-selects.
    if ($values instanceof Closure)
    {
        return $this->whereInSub($column, $values, $boolean, $not);
    }

    $this->wheres[] = compact('type', 'column', 'values', 'boolean');

    $this->bindings = array_merge($this->bindings, $values);

    return $this;
}

Look that it has a third boolean param. Good luck.

看看它有第三个布尔参数。祝你好运。

回答by Needpoule

You have a orWhereInfunction in Laravel. It takes the same parameters as the whereInfunction.

orWhereIn在 Laravel 中有一个函数。它采用与whereIn函数相同的参数。

It's not in the documentation but you can find it in the laravel API. http://laravel.com/api/4.1/

它不在文档中,但您可以在 laravel API 中找到它。 http://laravel.com/api/4.1/

That should give you this:

那应该给你这个:

$query-> orWhereIn('products.value', $f);

回答by Jason Lewis

Yes, orWhereInis a method that you can use.

是的,orWhereIn是一种您可以使用的方法。

I'm fairly sure it should give you the result you're looking for, however, if it doesn't you could simply use implodeto create a string and then explode it (this is a guess at your array structure):

我相当确定它应该给你你正在寻找的结果,但是,如果没有,你可以简单地使用implode创建一个字符串然后分解它(这是对你的数组结构的猜测):

$values = implode(',', array_map(function($value)
{
    return trim($value, ',');
}, $filters));

$query->whereIn('products.value', explode(',' $values));

回答by Abdul Rehman

$query = DB::table('dms_stakeholder_permissions');
$query->select(DB::raw('group_concat(dms_stakeholder_permissions.fid) as fid'),'dms_stakeholder_permissions.rights');
$query->where('dms_stakeholder_permissions.stakeholder_id','4');
$query->orWhere(function($subquery)  use ($stakeholderId){
            $subquery->where('dms_stakeholder_permissions.stakeholder_id',$stakeholderId);
            $subquery->whereIn('dms_stakeholder_permissions.rights',array('1','2','3'));
    });

 $result = $query->get();

return $result;

// OUTPUT @input $stakeholderId = 1

// 输出@input $stakeholderId = 1

//select group_concat(dms_stakeholder_permissions.fid) as fid, dms_stakeholder_permissionss.rightsfrom dms_stakeholder_permissionswhere dms_stakeholder_permissions.stakeholder_id= 4 or (dms_stakeholder_permissions.stakeholder_id= 1 and dms_stakeholder_permissions.rightsin (1, 2, 3))

//选择 group_concat(dms_stakeholder_permissions.fid) 作为 fid, dms_stakeholder_permissionss. rightsdms_stakeholder_permissions哪里dms_stakeholder_permissionsstakeholder_id= 4 或 ( dms_stakeholder_permissions. stakeholder_id= 1 and dms_stakeholder_permissions. rightsin (1, 2, 3))

回答by HARDIK

For example, if you have multiple whereIn OR whereIn conditions and you want to put brackets, do it like this:

例如,如果您有多个 whereIn 或 whereIn 条件并且您想放置括号,请这样做:

$getrecord = DiamondMaster::where('is_delete','0')->where('user_id',Auth::user()->id);
if(!empty($request->stone_id))
{
    $postdata = $request->stone_id;
    $certi_id =trim($postdata,",");
    $getrecord = $getrecord->whereIn('id',explode(",", $certi_id))
                           ->orWhereIn('Certi_NO',explode(",", $certi_id));     
}
$getrecord = $getrecord->get();