Debugbar and dd
| dd() stops | |
| dump() continues | |
| Debugbar concept | |
| Spot N+1 early |
dd() stops
dd means dump and die: it prints a variable in readable form and stops the request right there. Drop it into a controller when the code path confuses you, reload the page, read the output, then delete the line before you commit.
Because dd ends the request, lines below it never run. That property makes it a sharp scalpel for one question at a time: what does this variable hold at this exact point.
$userId = 7; dd($userId); echo "This line never runs";
dump() continues
dump prints the same readable output but lets the request finish. Use it when you want to compare several values across one page, for example the raw input and the validated copy side by side.
$name = "Ada"; dump($name); echo "The page still renders";
Debugbar concept
Debugbar is a development toolbar that sits at the bottom of your pages and counts what the request did: queries, views, route name, and timing. It never ships to production - it is a local lens for spotting waste early.
Debugbar panels to watch queries: every SQL statement and its time timeline: where the milliseconds went views: which templates rendered route: which route answered
Spot N+1 early
The classic N+1 problem hides in innocent loops: one query loads the list, then each row fires one more query for its relation. Debugbar shows the count climbing with the list size, which is the signature of this bug.
$posts = Post::all(); foreach ($posts as $post) { echo $post->author->name . "\n"; }
Ten posts run eleven queries here: one for the list plus one author lookup per row. Lesson 48 fixes it with a single eager load, but train your eyes now: loops plus relations deserve a second look.
Next: Broadcasting
Article author: Arthur Isaev