whereNotIn() and orWhereNotIn()

The whereNotIn() method lets you exclude rows where a column’s value is found in a given list. All values are securely bound using prepared statements, with additional input validation and error handling.

Signature:

public function whereNotIn(string $column, array $values): self
public function orWhereNotIn(string $column, array $values): self

Basic Usage

Exclude users with certain roles:

$db->whereNotIn('role', ['banned', 'inactive']);
$users = $db->get('users');

Empty Arrays Throw Exception

An empty array is considered invalid and will trigger an InvalidArgumentException.

$values = [];
$db->whereNotIn('status', $values); // ❌ Exception will be thrown

orWhereNotIn()

Same behavior as whereNotIn() but adds an OR clause instead:

$db->where('deleted_at', null);
$db->orWhereNotIn('role', ['superadmin']);

Allowed Input

  • [1, 2, 3] → ✅ valid
  • ['foo', 'bar'] → ✅ valid
  • [1, 'x'] → ⚠️ allowed but discouraged
  • [] → ❌ rejected (throws exception)
  • null, scalar or string → ❌ invalid, must be non-empty array

Security

Every value in the array is bound individually using parameterized queries to prevent injection. Mixed types are technically accepted but should be avoided for clarity and predictability.

See Also