Question

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
Was it helpful?

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.

OTHER TIPS

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. See the Laravel 8 orWhereIn documentation.

That should give you this:

$query-> orWhereIn('products.value', $f);
$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))

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

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

For example, if you have multiple whereIn OR whereIn conditions and you want to put brackets, do it like this:

$getrecord = DiamondMaster::where('is_delete','0')->where('user_id',Auth::user()->id);
if(!empty($request->stone_id))
{
    $postdata = $request->stone_id;
    $certi_id =trim($postdata,",");
    $getrecord = $getrecord->whereIn('id',explode(",", $certi_id))
                           ->orWhereIn('Certi_NO',explode(",", $certi_id));     
}
$getrecord = $getrecord->get();
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top