Skip to content

Laravel SQL Query Debugging: ->dd() and ->ddRawSql() Tips

Learn how to debug SQL queries in Laravel using ->dd(), ->ddRawSql(), ->dumpRawSql() and ->toSql() methods. See real examples in Artisan Tinker and stop wasting time on query logging boilerplate.

Dasun Kalupahana 2 min read 496 views 0 comments
On this page
  1. The Problem
  2. The Full Debugging Toolkit
  3. Conclusion

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 IN clause 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.

Ask the community

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.

Rate this post
0.0
Share

Enjoying the content?

If this article helped you, you can support the author with a coffee. It keeps new posts coming.

Buy me a coffee (opens in a new tab)

Comments 0

Be kind. Comments with links are reviewed before they appear.

No comments yet

Be the first to share your thoughts.

Get new articles in your inbox

Practical tutorials and deep dives, about once a week. No spam, unsubscribe anytime.

Privacy policy

Add DEV Talk to your Home Screen

  1. 1 Tap the Share button in Safari's toolbar.
  2. 2 Scroll down and tap Add to Home Screen.
  3. 3 Tap Add. Open it from the Home Screen to read like an app and to turn on notifications with the bell.

We use cookies to improve your experience and analyze traffic. By continuing, you agree to our Privacy Policy.