Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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 
like image 392
Laky Avatar asked Mar 31 '14 10:03

Laky


People also ask

What is wherein in laravel?

Laravel provide wherein() to use sql wherein query. in wherein() we just need to pass two argument one is column name and another if array of ids or anything that you want. You can see bellow syntax on wherein query in laravel: whereIn(Coulumn_name, Array);

What is difference between where and wherein in laravel?

Note: where will compare with just first value of array or just one single value. and whereIn will compare evey index of array.

What is use of with query in laravel?

Asking for help, clarification, or responding to other answers. Making statements based on opinion; back them up with references or personal experience.

What is Query Builder in laravel?

Laravel's database query builder provides a convenient, fluent interface to creating and running database queries. It can be used to perform most database operations in your application and works perfectly with all of Laravel's supported database systems.


2 Answers

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.

like image 81
Vit Kos Avatar answered Sep 22 '22 02:09

Vit Kos


You have a orWhereIn function in Laravel. It takes the same parameters as the whereIn function.

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

That should give you this:

$query-> orWhereIn('products.value', $f); 
like image 36
Needpoule Avatar answered Sep 24 '22 02:09

Needpoule