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

我的代码:

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

最佳答案

您可能只在核心中搜索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;
}

看起来它具有第三个 bool 参数。祝你好运。

10-06 14:39