In this tutorial, let's talk about a function that every Laravel developer has used, dd()
. I will show you various creative ways to use dd()
in our applications.
dd() In Collections
The first example is for Collections. It can also be an eloquent collection from the database. You can chain dd()
in the collections.
For example, you can have a collection with a map()
, filter()
, mapWitKeys()
, etc., and at the end, the dd()
to dump the result.
collect(['John Doe', 'Jane Doe']) ->map(/* your code */) ->filter(/* your code */) ->mapWithKeys(/* your code */) ->dd();
And at the end, the result is dumped.
Illuminate\Support\Collection {#287 ▼ // routes/web.php:17 #items: array:1 [▼ 0 => "John Doe" ] #escapeWhenCastingToString: false}
Link to the official documentation.
dd() In Benchmark
Since Laravel 9.32, there's a new way to test the performance of certain parts of your application. For such cases, you can use the Benchmark
helper class.
Benchmark::dd(fn () => User::find(1));
This will return results in milliseconds.
"8.262ms" // vendor/laravel/framework/src/Illuminate/Support/Benchmark.php:67
Or you may need to test different scenarios. For this case, in the dd
, you can pass an array.
Benchmark::dd([ 'Scenario 1' => fn () => User::count(), // 0.5 ms 'Scenario 2' => fn () => User::all()->count(), // 20.0 ms]);
Here, we get the result as an array for both scenarios.
array:2 [▼ // vendor/laravel/framework/src/Illuminate/Support/Benchmark.php:67 "Scenario 1" => "7.287ms" "Scenario 2" => "0.542ms"]
Link to the official documentation.
dd() For SQL Queries
Until Laravel 10.15, instead of getting the records from DB, we could use the toSql
method and dump the result.
$users = User::where('is_admin', false)->toSql();dd($users);
And the result would be the query but without the bindings.
"select * from `users` where `is_admin` = ?" // routes/web.php:20
Since Laravel 10.15 we have new methods toRawSql()
, dumpRawSql()
, and ddRawSql()
. So instead of toSql
, we can use toRawSql
.
$users = User::where('is_admin', false)->toRawSql();dd($users);
And now we have a query with the binding values.
"select * from `users` where `is_admin` = 0" // routes/web.php:20
Or, instead of doing dd($users)
, we can use ddRawSql
at the end of the query.
$users = User::where('is_admin', false)->ddRawSql();
The result is the same.
"select * from `users` where `is_admin` = 0" // vendor/laravel/framework/src/Illuminate/Database/Query/Builder.php:3945
You can check the pull request in the GitHub and about it in the official documentation.
Multiple Variables
Sometimes, you might need to dump more than one variable. You can do that by just separating variables with a comma.
$usersCount = User::where('is_admin', false)->count();$adminsCount = User::where('is_admin', true)->count();dd($usersCount, $adminsCount);
The result is dumped out twice.
52 // routes/web.php:21 48 // routes/web.php:21
Link to the official documentation.
dump() Instead Of dd()
There could be situations when you want to dump some info without stopping the rest of the execution of the application. For such cases, instead of dd
, use dump
.
Route::get('/', function () { $usersCount = User::where('is_admin', false)->count(); $adminsCount = User::where('is_admin', true)->count(); dump($usersCount, $adminsCount); return view('welcome');});
As a result, we have a whole page loaded, and our information is dumped at the top.
Link to the official documentation.
Extend Whole Object
When we dump the Eloquent Collection, we usually have an array of items.
When you click on an arrow, the model gets shown, but all other values are still collapsed.
If you want to extend everything at once, click on the arrow while holding the CTRL button.
Dumping In Blade
To dump some values in the Blade, there are two ways.
One way is to use the dd
helper in a curly bracket.
{{ dd($yourVariable) }}
The second way is to use a Blade directive @dd
.
@dd($yourVariable)
No comments or questions yet...