Problem
QueryBuilderInterface only exposes structured query methods (select, where, orderBy, etc.) and a low-level escape-hatch raw(). There is no way to inject a raw SQL expression into just the SELECT list, WHERE clause, or ORDER BY clause without dropping down to a full raw query. This blocks composite query specifications that need to mix structured conditions with expressions like COALESCE(a, b) or price > ?.
Proposed Solution
Add three raw-expression primitives to QueryBuilderInterface:
selectRaw(string $expression, array $bindings = []): static — appends a raw expression to the SELECT list, preserving any columns from select()
whereRaw(string $expression, array $bindings = []): static — AND-combines a raw condition with the regular WHERE clause; honored by aggregate methods
orderByRaw(string $expression, string $direction = 'ASC'): static — orders by an arbitrary expression
All three should validate the expression against a denylist (;, --, /*, */, backtick) and support positional ? bindings. Implement in MySqlQueryBuilder, PgSqlQueryBuilder, and RepositoryQueryBuilder.
Alternatives Considered
No response
Package
database
Problem
QueryBuilderInterfaceonly exposes structured query methods (select,where,orderBy, etc.) and a low-level escape-hatchraw(). There is no way to inject a raw SQL expression into just the SELECT list, WHERE clause, or ORDER BY clause without dropping down to a full raw query. This blocks composite query specifications that need to mix structured conditions with expressions likeCOALESCE(a, b)orprice > ?.Proposed Solution
Add three raw-expression primitives to
QueryBuilderInterface:selectRaw(string $expression, array $bindings = []): static— appends a raw expression to the SELECT list, preserving any columns fromselect()whereRaw(string $expression, array $bindings = []): static— AND-combines a raw condition with the regular WHERE clause; honored by aggregate methodsorderByRaw(string $expression, string $direction = 'ASC'): static— orders by an arbitrary expressionAll three should validate the expression against a denylist (
;,--,/*,*/, backtick) and support positional?bindings. Implement inMySqlQueryBuilder,PgSqlQueryBuilder, andRepositoryQueryBuilder.Alternatives Considered
No response
Package
database