How to use Laravel dd() method with less code

Categorized as Laravel Tagged ,

Hello, guys! Did you know that you don’t have to wrap your Eloquent query with dd() method, and just simple can add it at the end of the query? See how simple it is.

How to use dd() in Laravel

// ❌ Oldschool
dd(User::where('name', 'John')->get());

// ✅ Fancy
User::where('name', 'John')->get()->dd();

What is dd() in Laravel?

Dump and Die, or dd(), is a Laravel helper function that outputs information about a variable (or a query) and then stops the execution of the script.

You can use it for testing the value of a variable without having to stop and start the webserver or interrupt your workflow. Because dd() stops the script from running any further, it’s better only to use it when you’re sure you won’t need any code after the dd() statement.

In general, Dump and Die is mostly used for debugging purposes. When you’re trying to find an issue in your code, dd() helper can help you narrow down where the problem is by giving you a way to check the value of variables.

Leave a reply

Your email address will not be published.