Jonathan Bird
Jonathan Bird

Reputation: 352

Undefined variable $component with Livewire (possibly related to blade @component not being processed)

Always undefined variable in $component, however not always in the /vendor/filament/notifications/resources/views/notifications.blade.php view (although lately has been). I understand this seems to be related to Filament, however I've had this same issue with standard Livewire components so it appears to be anything Livewire related.

The line in the view is <?php $component->withAttributes(['channel' =>\Illuminate\View\Compilers\BladeCompiler::sanitizeComponentAttribute($broadcastChannel)]); ?> which Livewire injects. And on a different page, the error is with a different Livewire component with error on <?php $component->withAttributes([]); ?>.

When I dig into this, it's always that $component is not defined because the Blade itself isn't being parsed/processed. Eg this is the storage code (notice @component not being processed into $component = Illuminate\View\AnonymousComponent::resolve()):

@component('Illuminate\View\AnonymousComponent', 'filament-notifications::echo', ['view' => 'filament-notifications::components.echo','data' => ['channel' => $br>
<?php if (isset($attributes) && $attributes instanceof Illuminate\View\ComponentAttributeBag): ?>
<?php $attributes = $attributes->except(\Illuminate\View\AnonymousComponent::ignoredParameterNames()); ?>
<?php endif; ?>
<?php $component->withAttributes(['channel' => \Illuminate\View\Compilers\BladeCompiler::sanitizeComponentAttribute($broadcastChannel)]); ?>
@endComponentClass

It doesn't seem to be related to the codebase though as it will work on staging site (same server setup and codebase) but not work on production. And then you add a space to a view (no idea why this works) and redeploy and it's fixed.

Impossible to reproduce, however have had this issue on 6 different sites now. All different codebases, different AWS servers, only consistency is that they all run Livewire (version doesn't seem to make a difference), server managed on RunCloud and PHP 8.3. Nothing in any PHP or Nginx logs to indicate it's a mis-configuration.

Does not seem to be necessarily related to a deployment either. I had this crop up today after no deployment for 3 days. Restarting PHP & clearing OPCache which was my first thought did not solve it, neither did any composer update, changing Livewire or Filament versions, view:clear / cache, optimize:clear / optimize, composer dump-autoload, redeployment, restarting nginx/php or anything solve it.

Solution: The only thing that has ever solved it is to change one random view (eg add a space to the end of a random line of code) & redeploy and it magically solved it. Didn't work by adding a space to a Livewire backend file though. I'm not sure why changing a random view (not even a Livewire view) would fix it. I cleared & recached views but doesn't make a difference. I can usually solve most bugs easily but this is killing my brain so please any suggestions let me know.

Would love to find out what is actually causing this. I've researched as much as I can, and can't find anything. Can anyone please point me in the right direction, would be much appreciated!

Upvotes: 0

Views: 145

Answers (0)

Related Questions