Filter Integer Values with whereInt()

The whereInt() method adds a type-safe integer comparison to your SQL query. It ensures that the value passed is a true integer and not a string that could cause unexpected type casting. This helps avoid SQL injection risks or logic bugs, especially in ID comparisons.

Basic usage

By default, whereInt() uses the = operator:

$db->whereInt('id', 42);
$users = $db->get('users');

With custom operators

You can pass an optional third parameter to define a different operator like !=, >, < etc.

$db->whereInt('age', 18, '>=');
$adults = $db->get('users');

Using orWhereInt()

The counterpart orWhereInt() works exactly the same, but adds an OR condition instead of AND.

$db->whereInt('type', 1);
$db->orWhereInt('type', 2);
$rows = $db->get('accounts');

Invalid values are rejected

The whereInt() method rejects any value that is not a real integer – including strings like "123abc" or floats like 3.14. If an invalid value is passed, it will throw an exception and reset the query builder.

For other type-safe filters, see also whereString(), whereFloat(), whereBool() and whereDate().