gio
Estoy usando Laravel 5. Me gustaría saber cuáles son todas las variables pasadas a una vista dentro de la vista misma.
Dado que todas las variables están en el alcance de la vista, pensé que podría usar la función PHP genérica: get_defined_vars();
http://php.net/manual/en/function.get-defined-vars.php
Algo como esto:
// resources/view/home.blade.php
<html>
<body>
<?php print_r(get_defined_vars()); ?>
</body>
</html>
Pero me gustaría saber si hay una mejor manera (algo así como View::getData()
)
Nota: get_defined_vars() no funciona porque devuelve cientos de variables inútiles (componentes de Laravel)
Este es un fragmento (parcial) usando print_r(get_defined_vars())
(Creo que va en bucle de recursión infinita):
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] =>
)
limon monte
Utilizar el dd
ayudante:
{{ dd(get_defined_vars()) }}
Lee mas: https://laravel.com/docs/5.4/helpers#método-dd
Actualización (gracias, @JoeCoder): puede reducir aún más las variables “inútiles” haciendo:
{{ dd(get_defined_vars()['__data']) }}
-
No funciona, esa función devuelve cientos de variables inútiles. No puedo publicar una imagen (no tengo suficiente reputación), de lo contrario, podría mostrar
– giò
19 de marzo de 2015 a las 14:18
-
@llnk Si usas
dd
como se sugiere, esas variables “inútiles” se colapsan y solo terminan mostrando un puñado (de variables de nivel superior). Una manera mucho más fácil de ver los datos (a diferencia deprint_r
.– JoeCoder
19 de marzo de 2015 a las 14:19
-
@llnk acaba de probarlo, no hay “cientos de variables inútiles”: i.imgur.com/B6ciHRS.png
– Limón Monte
19 de marzo de 2015 a las 14:21
-
Puede reducir aún más las variables “inútiles” haciendo
dd(get_defined_vars()['__data'])
– JoeCoder
19 de marzo de 2015 a las 14:21
-
¡Entendido! Pensé
dd()
era un alias deprint_r
. Lo siento, no puedo votar esta respuesta porque no tengo suficiente reputación.– giò
19 de marzo de 2015 a las 14:23
León
Más o menos lo mismo, pero un poco más ordenado:
{{ dd($__data) }}
Raut vrushal
Usar la función de ayudante de Laravel dd
Usar dd
en vista de hoja:
{{ dd($__data) }}
O <?php dd($__data); ?>
Por encima de ambos métodos funciona en la vista de hoja.
Si está utilizando Laravel 5.1, que ahora permite extender Blade con directivas personalizadas, puede encontrarlo útil. Debe registrar directivas en AppServiceProvider como en este ejemplo o crea tu propio proveedor.
/**
* 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']"
);
});