I had anonymous component resourcesviewscomponentshomepagefeedback.blade.php
to render feedback on homepage. From the beginning it was just html. Then I decided to connect Class file. I already had another View Class component and I just copied it manually instead of using artisan command.
AppViewComponentsFeedback.php
namespace AppViewComponents; use IlluminateViewComponent; use AppModelsFeedback; class Feedback extends Component { public $feedbacks; public function __construct() { $this->feedbacks = Feedback::wherePublished(true)->take(5); } public function render() { return view('components.homepage.feedback'); } }
And then {{ dd($feedbacks) }} in view file gives me error that this variable is not defined.
Undefined variable: feedbacks (View: C:laragonwwwlara7resourcesviewscomponentshomepagefeedback.blade.php)
If I try to create Test component with artisan command and put this code inside it works, but then I cannot rename it back to Feedback class. It gives me error
SymfonyComponentErrorHandlerErrorFatalError Cannot declare class AppViewComponentsFeedback because the name is already in use
But old class already deleted, so I cannot understand what is wrong.
It seems like there is some hidden link between View Class and Blade components, which needs to be erased. But where is this link located?
Advertisement
Answer
I found problem.
I got $feedbacks is undefined
, because my anonymous component without variables initially was located in resourcesviewscomponentshomepagefeedback.blade.php
and when I decide to create View Class for this component there was no link established.
Laravel creates automatic link between feedback.blade.php
and appViewFeedbackComponent.php
only when blade file located directly in resourcesviewscomponents
folder. And my component was in subfolder.
So laravel tried to render resourcesviewscomponentshomepagefeedback.blade.php
with $feedback
variable inside and it cannot find where $feedback
is defined.
So I just manually register FeedbacksComponent class like that in appservice provider boot method
Blade::component('homepage-feedbacks', FeedbacksComponent::class);
and then use <x-homepage-feedbacks/>
to render it
I would say documentation is not very clear. It says that outside of components folder automatic discovery is not working. But it doesn’t say that inside components subfolder automatic discovery is not working.