TIL: Undefined variable when using inline Livewire component and PHP heredoc

I just started playing around with Livewire v3 and ran into an issue when making an inline component.

When using a PHP heredoc, there are actually two different syntaxes that have pretty drastically different behavior.


// Will throw undefined variable $string error.
$variable = <<<BLADE
This is evaluated the same as a PHP quoted {{ $string }}

$correct = <<<'BLADE'
This is returned entirely as a {{ $string }}

The first example is a "heredoc" while the second example is called a "newdoc".

For Livewire components, we want to use the newdoc syntax as this is what is eventually passed to the Blade compiler.

The docs for Livewire do infact show using the newdoc syntax, but since I hadn't used it much before, I was staring at my editor for a few minutes before I figured out what was going on!

Posted in:

Laravel Livewire