List all registered variables inside a Laravel view

PhpLaravelLaravel 5

Php Problem Overview


I am using Laravel 5. I would like to know which are all variables passed to a view inside the view itself.

Since all variables are in the view scope I thought I could use the generic PHP function: get_defined_vars(); http://php.net/manual/en/function.get-defined-vars.php

Something like this:

  // resources/view/home.blade.php
  <html>
  <body>
       <?php print_r(get_defined_vars()); ?>
  </body>
  </html>

But I would like to know if there is a better way (something like View::getData())

Note: get_defined_vars() deosn't work becausee it returns hundreds of useless variables (Laravel components)

This is a snippet (partial) using print_r(get_defined_vars()) (i think it goes in infinite recursion loop):

      Array
(
    [__path] => C:\net\laravel\storage\framework\views/8e030a77b0bdbacc2c4182fc04420d1d
    [__data] => Array
        (
            [__env] => Illuminate\View\Factory Object
                (
                    [engines:protected] => Illuminate\View\Engines\EngineResolver Object
                        (
                            [resolvers:protected] => Array
                                (
                                    [php] => Closure Object
                                        (
                                            [this] => Illuminate\View\ViewServiceProvider Object
                                                (
                                                    [app:protected] => Illuminate\Foundation\Application Object
                                                        (
                                                            [basePath:protected] => C:\net\laravel
                                                            [hasBeenBootstrapped:protected] => 1
                                                            [booted:protected] => 1
                                                            [bootingCallbacks:protected] => Array
                                                                (
                                                                    [0] => Closure Object
                                                                        (
                                                                            [static] => Array
                                                                                (
                                                                                    [instance] => Illuminate\Bus\BusServiceProvider Object
                                                                                        (
                                                                                            [defer:protected] => 1
                                                                                            [app:protected] => Illuminate\Foundation\Application Object
 *RECURSION*
                                                                                        )

                                                                                )

                                                                            [this] => Illuminate\Foundation\Application Object
 *RECURSION*
                                                                        )

                                                                    [1] => Closure Object
                                                                        (
                                                                            [static] => Array
                                                                                (
                                                                                    [instance] => Illuminate\Translation\TranslationServiceProvider Object
                                                                                        (
                                                                                            [defer:protected] => 1
                                                                                            [app:protected] => Illuminate\Foundation\Application Object
 *RECURSION*
                                                                                        )

                                                                                )

                                                                            [this] => Illuminate\Foundation\Application Object
 *RECURSION*
                                                                        )

                                                                )

                                                            [bootedCallbacks:protected] => Array
                                                                (
                                                                )

                                                            [terminatingCallbacks:protected] => Array
                                                                (
                                                                )

                                                            [serviceProviders:protected] => Array
                                                                (
                                                                    [0] => Illuminate\Events\EventServiceProvider Object
                                                                        (
                                                                            [app:protected] => Illuminate\Foundation\Application Object
 *RECURSION*
                                                                            [defer:protected] => 
                                                                        )

Php Solutions


Solution 1 - Php

Use the dd helper:

{{ dd(get_defined_vars()) }}

Read more: https://laravel.com/docs/5.4/helpers#method-dd

Update (thx, @JoeCoder): you can further cutdown on the "useless" variables by doing:

{{ dd(get_defined_vars()['__data']) }}

Solution 2 - Php

Kind of the same, but a bit tidier :

{{ dd($__data) }}

Solution 3 - Php

Use Laravel Helper function dd

Use dd in blade view:

{{ dd($__data) }} OR <?php dd($__data); ?>

Above both methods works in blade view.

Solution 4 - Php

If you are using Laravel 5.1 which now allows to extend Blade with custom directives you might find this useful. You need to register directives in AppServiceProvider like in this example or create you own provider.

     /**
     * Blade directive to dump template variables. Accepts single parameter
     * but also could be invoked without parameters to dump all defined variables.
     * It does not stop script execution.
     * @example @d
     * @example @d(auth()->user())
     */
    Blade::directive('d', function ($data) {
        return sprintf("<?php (new Illuminate\Support\Debug\Dumper)->dump(%s); ?>",
            null !== $data ? $data : "get_defined_vars()['__data']"
        );
    });

    /**
     * Blade directive to dump template variables. Accepts single parameter
     * but also could be invoked without parameters to dump all defined variables.
     * It works similar to dd() function and does stop script execution.
     * @example @dd
     * @example @dd(auth()->user())
     */
    Blade::directive('dd', function ($data) {
        return sprintf("<?php (new Illuminate\Support\Debug\Dumper)->dump(%s); exit; ?>",
            null !== $data ? $data : "get_defined_vars()['__data']"
        );
    });

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
Questiongi&#242;View Question on Stackoverflow
Solution 1 - PhpLimon MonteView Answer on Stackoverflow
Solution 2 - PhpLeonView Answer on Stackoverflow
Solution 3 - PhpVrushal RautView Answer on Stackoverflow
Solution 4 - Phpuser2920996View Answer on Stackoverflow