whereIn() and orWhereIn()
The whereIn()
method allows you to filter rows based on whether a column's value is found within a given list.
It safely binds all values via prepared statements and rejects invalid input structures early.
Signature:
public function whereIn(string $column, array $values): self
public function orWhereIn(string $column, array $values): self
Basic Usage
Filter users by multiple IDs:
$db->whereIn('id', [1, 2, 3]);
$users = $db->get('users');
String Lists
Also works for string values:
$db->whereIn('status', ['active', 'pending']);
$results = $db->get('orders');
Empty Arrays Are Not Allowed
If an empty array is passed, an InvalidArgumentException
is thrown.
This prevents unnoticed logic errors and guarantees query integrity.
$ids = [];
$db->whereIn('id', $ids); // ❌ Will throw an exception
To avoid this, always check the input beforehand:
if (!empty($ids)) {
$db->whereIn('id', $ids);
}
orWhereIn()
Works exactly like whereIn()
but uses an OR
condition instead of AND
.
$db->where('status', 'archived');
$db->orWhereIn('type', ['internal', 'system']);
Allowed Input
[1, 2, 3]
→ ✅ valid integers['a', 'b', 'c']
→ ✅ valid strings[1, 'x']
→ ⚠️ mixed types allowed, but discouraged[]
→ ❌ empty → will throw exceptionnull
or scalar → ❌ invalid, must be array
Security
All elements of the array are passed through internal validation and bound using prepared statements. Mixed-type arrays are accepted but discouraged to avoid logic ambiguity.