Laravel whereIn OR whereIn
Asked Answered
L

7

41

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
Lunna answered 31/3, 2014 at 10:6 Comment(1)
yes and it is exactly called as orWhereIn. dude, you should have given it a try 1st.Slain
B
53

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.

Bibliopole answered 31/3, 2014 at 10:15 Comment(0)
S
47

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 11 orWhereIn documentation.

That should give you this:

$query-> orWhereIn('products.value', $f);
Spine answered 31/3, 2014 at 10:15 Comment(3)
@HassanAzimi Your comment is wrong and misleading. [link] (laravel.com/api/5.4/Illuminate/Database/Query/…)Pape
@Pape That link doesnt work anymore, and I cant seem to find it anymore: laravel.com/api/8.x/Illuminate/Database/Eloquent/Builder.htmlInformed
laravel.com/api/8.x/Illuminate/Database/Query/… It's in Database/Query and not Database/EloquentSpine
G
8

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));
Gonagle answered 31/3, 2014 at 10:17 Comment(0)
W
8
$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))

Wariness answered 12/2, 2015 at 7:9 Comment(0)
C
6

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

Circumspection answered 27/3, 2021 at 8:15 Comment(0)
R
1

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();
Reiko answered 8/6, 2017 at 4:11 Comment(0)
W
-2

Example 1: Eloquent Wherein Query Using Laravel Model With Different Column Name

public function index()
{
    $data= User::whereIn('name', ['john','dam','smith'])->get();
  
    dd($data);                    
}

Example 2: Filtering Posts by Categories Using Wherein Suppose you have a posts table with a category_id column, and you want to retrieve posts from specific categories. You have an array of category IDs you’re targeting:

$categoryIds = [2, 4, 6];

$posts = Post::whereIn('category_id', $categoryIds)->get();

Example 3: Retrieving Users by IDs using Eloquent Wherein Query Let’s say you have a users table and you want to retrieve users by their IDs. You have an array of IDs you’re interested in:

$userIds = [1, 3, 5, 7];

$users = User::whereIn('id', $userIds)->get();

Check here in more detail . https://woolocker.com/page/how-to-laravel-eloquent-wherein

Willpower answered 14/11, 2023 at 15:28 Comment(1)
If you are affiliated with a linked website, you must disclose your affiliation. Otherwise, your answer could be deleted for spam. Keep in mind that excessively promoting the same website can also get your answers deleted. Please see how to not be a spammer.Phillis

© 2022 - 2024 — McMap. All rights reserved.