Skip to content

Instantly share code, notes, and snippets.

@victorloux
Last active October 12, 2020 10:52
Show Gist options
  • Save victorloux/0c073afa5d4784d2b8e9 to your computer and use it in GitHub Desktop.
Save victorloux/0c073afa5d4784d2b8e9 to your computer and use it in GitHub Desktop.
@Dump directive for Blade in Laravel 5
// place the following in app/Providers/AppServiceProviders.php, in the boot() method
/**
* Blade directive to dump a variable/object inside a template.
* This is similar to dd(), except that it doesn't interrupt the
* execution of the app. It does NOT support multiple arguments
* however, you have to use one directive per variable.
*
* @example @dump($posts->comments)
*/
Blade::directive('dump', function($param) {
return "<pre><?php (new \Illuminate\Support\Debug\Dumper)->dump{$param}; ?></pre>";
});
@imabug
Copy link

imabug commented Aug 31, 2017

should be dump($param) instead of dump{$param}?

@victorloux
Copy link
Author

@imabug no, because {$param} gets changed to ($posts->comments) including the parentheses, so otherwise it'd compile to ->dump(($posts->comments)) and that caused issues iirc

(but that gist is from 2 years ago so the Laravel API might have changed, I need to test it again)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment