
📌 Overview
When working with Laravel's Eloquent or Query Builder, it's often helpful to inspect the raw SQL queries being executed, especially for debugging or performance optimization. Below are four effective ways to print SQL queries in Laravel:
1: ddRawSQL()
Prints the full raw SQL query with parameters included and stops execution.
$user = User::where('email', 'user@gmail.com')->ddRawSQL();
OutPut:
select * from users where email = 'user@gmail.com'
2: dd()
Prints the SQL query with placeholders and shows the parameter bindings separately. Execution is stopped.
$user = User::where('email', 'user@gmail.com')->dd();
Output:
select * from users where email = ?
array:1 [
0 => "user@gmail.com"
]
3: toSql()
Prints the SQL query with placeholders only. Execution continues.
$sql = User::where('email', 'user@gmail.com')->toSql();
echo $sql;
Output:
select * from users where email = ?
4: toRawSql()
Return the raw SQL query with parameters replaced in-place. Execution continues.
$sql = User::where('email', 'user@gmail.com')->toRawSql();
echo $sql;
Output:
select * from users where email = 'user@gmail.com'
* Please Don't Spam Here. All the Comments are Reviewed by Admin.