{{dump('string')}} in Laravel blade outputs value twice
Asked Answered
C

2

7

Using Laravel 5.5.34, I have trouble outputting debug information in Blade templates using the dump() helper.

{{ dump('test') }}

results in the following output:

screenshot

I wouldn't expect the raw string "test" to show up below the actual debug output. Is this the normal behavior and if yes, how can I disable it? If no, what misconfiguration could cause it?

Chichi answered 11/2, 2018 at 12:32 Comment(7)
why do you dump test if you dont want to dump it?Cheiron
@Cheiron I would like to see the upper output. The part with the black background. However, I don't want the second "test" to show.Chichi
Do you have any code after the dump()? dump() doesn't exit; after its execution, as does dd() (dump and die).Winterwinterbottom
@Winterwinterbottom I'm not aware of any code being executed after the dump() statement. If I remove the dump() call altogether, both outputs disappear.Chichi
If you do <?php dump('test'); ?>, does it change anything ?Brazier
@AlexvanVliet Yes, that removes the second unwanted output. If all things fail, i'll use that as a workaround, but I'm still curious as to why the dump() helper method produces the wrong output when used in Blade statements. :)Chichi
@Chichi Then if I'm not mistaken, dump must return the value and the {{}} print itBrazier
C
12

Digging a little deeper, I found the source of the problem.

First of all, Blade translates this...

{{ dump($var) }}

to this:

<?php echo e(dump($var)); ?>

That has always worked fine, because Symfony's dump() helper has never returned a value. However, they have changed that with this commit: https://github.com/symfony/var-dumper/commit/b6d0c8cd9949a5de4e71413e6ffbc2ea9dcb647f#diff-2e42573e053ced723652b17a395226f0

Since then, dump() does return $var!

Because Laravel uses this aforementioned dump() helper from symfony/var-dumper, e() will now suddenly receive $var back from dump().

This is the e() helper function used in Laravel:

function e($value, $doubleEncode = false)
{
    if ($value instanceof Htmlable) {
        return $value->toHtml();
    }

    return htmlspecialchars($value, ENT_QUOTES, 'UTF-8', $doubleEncode);
}

This causes the double output in case $var is of type string, and throws an exception if it isn't, because htmlspecialchars() only accepts strings as first argument.

The solution was to create my own dump helper that doesn't contain the return statement you can see in the diff.

Chichi answered 11/2, 2018 at 13:58 Comment(3)
Maybe you could go even further and add your own @dump ?Brazier
@AlexvanVliet Actually I prefer using a helper function. It works as expected now. Thank you very much, by the way, one of your comments got me on the right track to finding the solution. :)Chichi
Note that @dump directive (Laravel 5.6) doesn't have this problem because it's not use echoCacique
G
7

In newer versions of Laravel, you can use the @dump() directive.

Grimsby answered 12/11, 2022 at 18:0 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.