Raw queries
All statements that can be built using the methods exposed by this library support the ability to add custom SQL fragments to your query.
The raw()
method is available on the Statement
class, which means you can use it in any query builder that extends Statement
.
Basic usage
$query->raw("LIMIT %s OFFSET %s", [10, 20]);
This adds a LIMIT
and OFFSET
clause with placeholders, and the values 10
and 20
will be bound to these placeholders.
Chaining
The raw method returns the Statement
object, so you can continue chaining other query builder methods after it.
$query->raw("LIMIT %s OFFSET %s", [10, 20])
->raw("WHERE %s = %s", ['id', 1]);
Use raw when you need to add SQL fragments that are not directly supported by the query builder's methods.
Security
Remember to be cautious when using raw, as it bypasses the query builder's safeguards. Always properly sanitize any user input used in raw SQL to prevent SQL injection vulnerabilities.