4

I was wondering if there is a way to use whereIn with an array of arrays, something like the where with array

User::where(['user_id' => $currentUser, 'group_id' => $currentGroup]);

so i'm looking for this:

User::whereIn(['group' => $availableGroups, 'session' => $currentSessions]).

which should be equivalent to using the whereIn clause twice.

I was thinking of solving this like:

foreach (query as $key => $value)
   $userQuery->whereIn($key, $value);

I was wondering if there is a better way.

1 Answer 1

4

No that's not possible. You have to use whereIn() multiple times (or in a loop).

There's no logic that would handle such a parameter correctly:

public function whereIn($column, $values, $boolean = 'and', $not = false)
{
    $type = $not ? 'NotIn' : 'In';

    // ... irrelevant code omitted ...

    $this->wheres[] = compact('type', 'column', 'values', 'boolean');

    $this->addBinding($values, 'where');

    return $this;
}
Sign up to request clarification or add additional context in comments.

2 Comments

I don't understand what you mean by "no logic that would handle ..."
The code I posted is from the framework core. If an array of arrays was supported, the method would contain logic to handle that. Probably call whereIn for every subarray... I added this code to prove my statement.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.