schel4ok
schel4ok

Reputation: 684

hidden magic in Laravel blade components

I had anonymous component resources\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?

Upvotes: 1

Views: 4141

Answers (4)

Anton Oliinyk
Anton Oliinyk

Reputation: 186

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.

Upvotes: 5

Pero
Pero

Reputation: 41

In Laravel 8 you can use and no need to declare the component

<x-homepage.feedback />

Upvotes: 2

ZayR
ZayR

Reputation: 59

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

Upvotes: 0

schel4ok
schel4ok

Reputation: 684

I found problem.

I got $feedbacks is undefined, because my anonymous component without variables initially was located in resources\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 between feedback.blade.php and app\View\FeedbackComponent.php only when blade file located directly in resources\views\components folder. And my component was in subfolder.

So laravel tried to render resources\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.

Upvotes: 2

Related Questions