Laravel whereIn OR WhereIn

2022-08-30 12:48:11

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

我的代码:

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

查询:

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

但我需要:

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

在Laravel中是否有类似的东西:

orWhereIn

答案 1

你可以只搜索核心中的功能来查看它。给你。这必须回答您的所有问题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;
}

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


答案 2

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

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

这应该给你这个:

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

推荐