Skip to content
Advertisement

List all registered variables inside a Laravel view

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:netlaravelstorageframeworkviews/8e030a77b0bdbacc2c4182fc04420d1d
    [__data] => Array
        (
            [__env] => IlluminateViewFactory Object
                (
                    [engines:protected] => IlluminateViewEnginesEngineResolver Object
                        (
                            [resolvers:protected] => Array
                                (
                                    [php] => Closure Object
                                        (
                                            [this] => IlluminateViewViewServiceProvider Object
                                                (
                                                    [app:protected] => IlluminateFoundationApplication Object
                                                        (
                                                            [basePath:protected] => C:netlaravel
                                                            [hasBeenBootstrapped:protected] => 1
                                                            [booted:protected] => 1
                                                            [bootingCallbacks:protected] => Array
                                                                (
                                                                    [0] => Closure Object
                                                                        (
                                                                            [static] => Array
                                                                                (
                                                                                    [instance] => IlluminateBusBusServiceProvider Object
                                                                                        (
                                                                                            [defer:protected] => 1
                                                                                            [app:protected] => IlluminateFoundationApplication Object
 *RECURSION*
                                                                                        )

                                                                                )

                                                                            [this] => IlluminateFoundationApplication Object
 *RECURSION*
                                                                        )

                                                                    [1] => Closure Object
                                                                        (
                                                                            [static] => Array
                                                                                (
                                                                                    [instance] => IlluminateTranslationTranslationServiceProvider Object
                                                                                        (
                                                                                            [defer:protected] => 1
                                                                                            [app:protected] => IlluminateFoundationApplication Object
 *RECURSION*
                                                                                        )

                                                                                )

                                                                            [this] => IlluminateFoundationApplication Object
 *RECURSION*
                                                                        )

                                                                )

                                                            [bootedCallbacks:protected] => Array
                                                                (
                                                                )

                                                            [terminatingCallbacks:protected] => Array
                                                                (
                                                                )

                                                            [serviceProviders:protected] => Array
                                                                (
                                                                    [0] => IlluminateEventsEventServiceProvider Object
                                                                        (
                                                                            [app:protected] => IlluminateFoundationApplication Object
 *RECURSION*
                                                                            [defer:protected] => 
                                                                        )

Advertisement

Answer

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']) }}
User contributions licensed under: CC BY-SA
4 People found this is helpful
Advertisement