Learn how to easily retrieve the raw SQL query generated by Laravel's query builder as a string in your PHP code.
Sometimes you need to see the raw SQL query that your Laravel Query Builder instance will execute. This can be useful for debugging, logging, or simply understanding how your code translates to SQL. Here's how to get the raw SQL query from a Laravel Query Builder object:
Start with your Laravel Query Builder instance:
$query = DB::table('users')->where('active', 1);
Access the SQL query and bindings:
$sql = $query->toSql();
$bindings = $query->getBindings();
Replace the placeholders in the SQL query with the actual bindings:
$rawQuery = str_replace(
array_map(fn($v) => "?", $bindings),
array_map(fn($v) => is_string($v) ? "'$v'" : $v, $bindings),
$sql
);
Now, $rawQuery
will contain the raw SQL query string that would be executed. For example:
"select * from `users` where `active` = '1'"
This PHP code demonstrates how to convert a Laravel Query Builder instance into a raw SQL query string with values injected. It first builds a query to select active users. Then, it extracts the SQL query and bindings from the Query Builder. Finally, it replaces placeholders in the SQL query with the actual values from the bindings, resulting in a complete raw SQL query ready for execution. However, this approach is highly vulnerable to SQL injection and should not be used with user-provided input.
<?php
use Illuminate\Support\Facades\DB;
// 1. Start with your Laravel Query Builder instance
$query = DB::table('users')->where('active', 1);
// 2. Access the SQL query and bindings
$sql = $query->toSql();
$bindings = $query->getBindings();
// 3. Replace the placeholders in the SQL query with the actual bindings
$rawQuery = str_replace(
array_map(fn($v) => "?", $bindings),
array_map(fn($v) => is_string($v) ? "'$v'" : $v, $bindings),
$sql
);
// Output the raw SQL query
echo $rawQuery; // Output: "select * from `users` where `active` = '1'"
?>
Explanation:
Building the Query:
users
table.where('active', 1)
clause filters for users where the active
column is equal to 1.Extracting SQL and Bindings:
$query->toSql()
: Retrieves the raw SQL query string with placeholders ("?") for bindings.$query->getBindings()
: Gets an array of values that will replace the placeholders.Constructing the Raw Query:
array_map(fn($v) => "?", $bindings)
: Creates an array of "?" placeholders matching the number of bindings.array_map(fn($v) => is_string($v) ? "'$v'" : $v, $bindings)
: Processes each binding:
str_replace()
: Replaces the "?" placeholders in the $sql
string with the processed bindings.Important Note:
dd()
and dump()
: While Laravel's debugging helpers like dd()
and dump()
can display query information, they don't always show the fully constructed SQL query, especially with complex joins and subqueries. This method provides a more direct way to see the final SQL.toSql()
and getBindings()
on an Eloquent query builder instance as well.Caution:
Feature | Description |
---|---|
Goal | Get the raw SQL query string that Laravel's Query Builder would execute. |
Steps | 1. Create a Query Builder instance. 2. Use toSql() to get the parameterized SQL query. 3. Use getBindings() to get the values for the placeholders. 4. Replace the placeholders in the SQL query with the actual bindings. |
Example |
DB::table('users')->where('active', 1) becomes "select * from userswhere active = '1'"
|
Note | This process essentially "compiles" the Query Builder instance into a plain SQL string. |
This article explained how to get the raw SQL query from a Laravel Query Builder object for debugging or logging purposes. While this approach can be helpful in understanding how Laravel constructs SQL queries, it's crucial to remember its limitations. Directly injecting values into raw SQL strings is highly susceptible to SQL injection vulnerabilities and should never be done with user-provided input. Always rely on Laravel's parameterized queries or Eloquent ORM for secure database interactions. This method is best suited for development and debugging, providing insights into the underlying SQL generated by your Laravel code.