whereFunc()

The whereFunc() and orWhereFunc() methods allow you to use raw SQL expressions or database functions in your WHERE clause – while maintaining full parameter binding on the comparison side.

Signature:

public function whereFunc(string $expression, mixed $value, string $operator = '='): self
public function orWhereFunc(string $expression, mixed $value, string $operator = '='): self

Usage Example

Compare the current year using YEAR():

$db->whereFunc('YEAR(created_at)', 2024);
$users = $db->get('users');

Using a different operator:

$db->whereFunc('LENGTH(username)', 10, '>=');
$users = $db->get('users');

Security Notice

The expression (first argument) is used raw and must be a trusted, valid SQL fragment. The $value is still securely bound via prepared statements. If the expression contains disallowed aggregate functions like SUM() or AVG(), it will be rejected.

Only scalar values are allowed as $value. The expression is not validated or escaped automatically – use with care.

OR Variant

orWhereFunc() behaves exactly like whereFunc(), but adds an OR condition instead of AND.

See Also