Search code examples
phpmysqllaravellaravel-4query-builder

Laravel whereIn OR whereIn


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:

orWhereIn

Solution

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

    /**
     * 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.