List all registered variables inside a Laravel view

前端 未结 5 1460
故里飘歌
故里飘歌 2020-12-12 19:09

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 cou

相关标签:
5条回答
  • 2020-12-12 19:25

    Kind of the same, but a bit tidier :

    {{ dd($__data) }}

    0 讨论(0)
  • 2020-12-12 19:28

    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']"
            );
        });
    
    0 讨论(0)
  • 2020-12-12 19:34

    For better readability and debugging purposes, you can also create a helper which turns output into an array.

    // as per comment from Braunson add this to custom helpers function in app\helpers.php and include it via composer.
    
    if (! function_exists('da')) {
        /**
         * Dump the passed variables to array and end the script.
         *
         * @param  mixed
         * @return void
         */
        function da()
        {
            array_map(function ($x) {
                dd($x->toArray());
            }, func_get_args());
        }
    }
    
    0 讨论(0)
  • 2020-12-12 19:40

    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']) }}
    
    0 讨论(0)
  • 2020-12-12 19:45

    Use Laravel Helper function DD

    Use DD in blade view:

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

    Above both methods works in blade view.

    0 讨论(0)
提交回复
热议问题