In this guide, you’ll learn how to get the raw SQL query in Laravel, including print-ready queries with actual values, and how to log SQL queries in Laravel for debugging.

When working with complex queries in Laravel, it's often helpful to see the actual SQL query generated by the query builder. Whether you're debugging performance issues or just want to log SQL queries for optimization, Laravel makes it easy to view the raw SQL query — with or without bindings.
Debugging complex queries? Want to see the actual SQL query with data? Here's how you can get the print-ready SQL query from Laravel's Query Builder.
1: Get the SQL Query With Placeholders Using toSql()
Laravel's toSql()
method is useful when you just need the query structure.
This returns:
This is helpful for understanding the query structure, but it doesn’t include the bound values. If you're trying to print the full SQL query in Laravel, you’ll need a bit more.
2: Get Full SQL With Bound Values (Print-Ready SQL Query)
To Get SQL query With Bindings (Manually Merged) or print the full SQL query with bindings (actual values), use the following method:
This will output:
This approach is very helpful for debugging Laravel SQL queries or sharing them in logs or reports.
This technique is commonly used by Laravel developers who want to:
- Debug database queries
- Optimize Laravel Eloquent or Query Builder performance
- Log complete SQL queries to a file
Bonus: Log All SQL Queries in Laravel
If you want to automatically log every SQL query in Laravel, including the actual bound values, add the following code to your App\Providers\AppServiceProvider
inside the boot()
method:
Now, all your Laravel database queries will be logged to storage/logs/laravel.log
, which is great for debugging, performance monitoring, or auditing queries.
Why This Matters
Using this technique, you can:
- Understand exactly what SQL Laravel is executing behind the scenes
- Track down issues when Laravel queries return unexpected results
- Optimize slow queries by analyzing the raw SQL and indexing strategy
- Keep logs of all executed queries in a Laravel application
Whether you're using Eloquent ORM or Laravel Query Builder, this tip helps you gain more control and visibility over your database layer.
Final Thoughts
Knowing how to print raw SQL queries with values in Laravel is a powerful skill. It’s especially useful for:
- Laravel backend developers
- Full-stack developers optimizing database interactions
- Teams that need better SQL debugging in Laravel
By combining toSql()
with getBindings()
and using vsprintf()
, you can easily debug Laravel queries, log them, and even run them manually for deeper analysis.