Laravel Debugging with dd and Logs
Debugging means finding why code behaves the wrong way. dd and the log file are two tools for that job.
Stopping with dd
The dump page shows the string.
dd('Debug value');Carrying on with dump
The page still returns its answer after the dump.
dump('Dumped value');
return 'After dump';Writing to the log
The route answers Logged, and the line waits in the log file. The log file then holds the line Lesson 47 log line.
Log::info('Lesson 47 log line');
return 'Logged';Example
use Illuminate\Support\Facades\Route;
Route::get('/l47/dd', function () {
dd('Debug value');
});Key points
- Debugging: finding why code behaves the wrong way.
- Use dd to print a value and stop the request.
- Use dump to print a value and carry on.
- Use the log to write a line you can read later.
In this example, the route calls dd with a short string. dd prints the string in a dump page and stops the request. Nothing after dd runs. Debugging means finding why code behaves the wrong way. Visit /l47/dd in the browser to see the output.
Note
dd stops the request, so remove it before you ship the code.
Try it Yourself
Edit the code, then press Run.
Running is simulated
Exercise
What is the difference between dd and dump?