On this page
When building Laravel applications, one of the most common frustrations is not knowing what SQL query Eloquent is actually generating under the hood. Most developers reach for DB::enableQueryLog() or install a debugbar package — but there's a much faster way built right into Laravel.
The Problem
You write an Eloquent query like this:
$users = User::where('active', 1)
->orderBy('created_at', 'desc')
->get();
But something is off with the results. You want to see the raw SQL being executed — without adding a package, without enabling query logging, and without leaving your terminal.
Solution 1: ->dd()
Append ->dd() instead of ->get() and Laravel will dump the query results and die — stopping execution right there and printing everything to the screen.
User::where('active', 1)
->orderBy('created_at', 'desc')
->dd();
This is the equivalent of wrapping your result in dd() but without needing a separate variable. Clean, fast, one less line.
Solution 2: ->ddRawSql()
This is the hidden gem. Instead of dumping the results, ->ddRawSql() dumps the fully compiled raw SQL query with all bindings already substituted in — exactly what gets sent to your database.
User::where('active', 1)
->orderBy('created_at', 'desc')
->ddRawSql();
Output in Tinker:
SELECT * FROM `users`
WHERE `active` = 1
ORDER BY `created_at` DESC
No placeholders. No ? marks. The real query, ready to copy and paste directly into your database client.
The Full Debugging Toolkit
Laravel actually gives you a whole family of these methods:
|
Method |
What it does |
|---|---|
|
->dd() |
Dumps query results and dies |
|
->dump() |
Dumps results, continues execution |
|
->ddRawSql() |
Dumps raw SQL with bindings and dies |
|
->dumpRawSql() |
Dumps raw SQL with bindings, continues |
|
->toSql() |
Returns SQL string with ? placeholders |
|
->getBindings() |
Returns array of binding values |
Using It in Artisan Tinker
These methods shine brightest inside Artisan Tinker — Laravel's interactive REPL. Fire it up and debug queries interactively without touching your application code:
php artisan tinker
// Inside Tinker:
User::where('active', 1)->ddRawSql();
// "select * from `users` where `active` = 1"
Post::with('comments')
->where('published', true)
->latest()
->ddRawSql();
// Full query with JOIN revealed instantly
This is especially useful for debugging eager loading — you can see exactly how Laravel constructs the
WHERE INclause for related models.
Quick Tip: Don't Forget ->dumpRawSql()
If you want to inspect the SQL but not stop execution — useful when debugging inside a loop or a seeder — use ->dumpRawSql() instead:
foreach ($segments as $segment) {
Order::where('segment_id', $segment->id)
->dumpRawSql(); // prints and continues
}
When to Use What
- Debugging a single query fast →
->ddRawSql() - Want to copy the query to your DB client →
->ddRawSql() - Debugging inside a loop →
->dumpRawSql() - Just want to see result data →
->dd() - Logging queries programmatically →
->toSql()+->getBindings()
Conclusion
You don't need a package, a debugbar, or five lines of query logging boilerplate. Laravel's built-in ->dd(), ->ddRawSql(), and their dump siblings give you instant SQL visibility right from Tinker or anywhere in your codebase.
Next time a query misbehaves — append ->ddRawSql() and the mystery is solved in seconds.
Free resources for this topic
Code snippets and fixes for this topic
Get one practical tip every week
New tutorials, fixes and free cheat sheets from DEV Talk, straight to your inbox. No spam, unsubscribe in one click.
Free download: Laravel interview questions (PDF)
Have a question about this post?
Ask the community: other developers and the author can answer.
Get notified about new articles
One click, no email needed. Turn it off anytime with the bell at the top.
Get new articles on your iPhone
Add DEV Talk to your Home Screen, open it, then tap the bell.
Enjoying the content?
If this article helped you, you can support the author with a coffee. It keeps new posts coming.
Comments 0
No comments yet
Be the first to share your thoughts.