php - hidden magic in Laravel blade components
Get the solution ↓↓↓I had anonymous componentresources\views\components\homepage\feedback.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.
App\View\Components\Feedback.php
namespace App\View\Components;
use Illuminate\View\Component;
use App\Models\Feedback;
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:\laragon\www\lara7\resources\views\components\homepage\feedback.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
Symfony\Component\ErrorHandler\Error\FatalError
Cannot declare class App\View\Components\Feedback 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?
Answer
Solution:
When switching component type from anonymous to class and back, you have to clear compiled view files:
php artisan view:clear
That's because Laravel incorporate specific component type invocation into the compiled view code.
Answer
Solution:
In Laravel 8 you can use and no need to declare the component
<x-homepage.feedback />
Answer
Solution:
I found problem.
I got$feedbacks is undefined
, because my anonymous component without variables initially was located inresources\views\components\homepage\feedback.blade.php
and when I decide to create View Class for this component there was no link established.
Laravel creates automatic link betweenfeedback.blade.php
andapp\View\FeedbackComponent.php
only when blade file located directly inresources\views\components
folder. And my component was in subfolder.
So laravel tried to renderresources\views\components\homepage\feedback.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.
Answer
Solution:
I think you're right I have been having the same issue and and I've been really struggling with it. Finally I found a workaround which is if you change the file name it works so I think it's a problem with the laravel framework and I think they need to address this issue
Share solution ↓
Additional Information:
Link To Answer People are also looking for solutions of the problem: fastcgi sent in stderr: "primary script unknown" while reading response header from upstream
Didn't find the answer?
Our community is visited by hundreds of web development professionals every day. Ask your question and get a quick answer for free.
Similar questions
Find the answer in similar questions on our website.
Write quick answer
Do you know the answer to this question? Write a quick response to it. With your help, we will make our community stronger.