Laravel whereIn OR whereIn

186,086

Solution 1

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.

Solution 2

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

Solution 3

$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

//select group_concat(dms_stakeholder_permissions.fid) as fid, dms_stakeholder_permissionss.rights from dms_stakeholder_permissions where dms_stakeholder_permissions.stakeholder_id = 4 or (dms_stakeholder_permissions.stakeholder_id = 1 and dms_stakeholder_permissions.rights in (1, 2, 3))

Solution 4

Yes, orWhereIn is a method that you can use.

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

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

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

Solution 5

Yes, orWhereIn where clause method exists in Laravel. Please see the below link of official Laravel Query Builder documentation for detailed information.

Documentation Link: https://laravel.com/docs/8.x/queries#additional-where-clauses

There are two ways to get the below output.

and (products.value in (Bomann, PHILIPS) OR products.value in (red,white))
  1. Using orWhereIn
$query = Product::where('color', 'blue')
    ->whereIn('value', ['Bomann', 'PHILIPS'])
    ->orWhereIn('value', ['red', 'white'])
    ->get();

Output:

select * from `products` where `color` = 'blue' and `value` in (Bomann, PHILIPS) OR `value` in (red,white)) 
  1. Using orWhere and whereIn
$query2 = Product::where('color', 'blue')
    ->whereIn('value', ['Bomann', 'PHILIPS'])
    ->orWhere(function ($query) {
        $query->whereIn('value', ['Bomann', 'PHILIPS']);
    })
    ->get();

Output:

select * from `products` where `color` = 'blue' and `value` in (Bomann, PHILIPS) OR (`value` in (red,white)) 

Share:
186,086
Laky
Author by

Laky

Updated on March 27, 2021

Comments

  • Laky
    Laky about 3 years

    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
    
  • ofumbi
    ofumbi almost 6 years
    @HassanAzimi Your comment is wrong and misleading. [link] (laravel.com/api/5.4/Illuminate/Database/Query/…)