CodezPoet's avatar

Component blade.php template stops working in subfolder, works in main components folder

Hi,

Does anyone know how to solve this? I have tried all kinds of things, and searched, and cannot solve this, so posting here in the hope the community knows.

Summary

The component was made with Artisan. The component works fine if in the head folder. I have other components in the same subfolder and those work fine (but they don't call code like this, but pass variable directly for example (:variable=$variable). I have tried all kinds of possible solution, remade it, searched, asked the AI, restarded server, cleared caches, tried different namespaces and folders, etc. I have tried dd, different types of code. It just looks like when moving to subfolder it loses the connection with the component class, like the two are not aware of each other anymore maybe.

Goal:

I want to have things like dynamic page headers based on the section where at. So I can reuse code and not have so many different templates, for each section.

The error it shows when you move the compoment blade.php to a subfolder is:

Undefined variable $headerTitle

It will still load the template and load the static text. Just it can't process the $headerTitle variable for a reason I cannot figure out.

The component is called like this:

<x-app-layout>
    <x-slot name="header" id="dashboard-records-header">
        <x-forms.records.header-records />
    </x-slot>
 // code

Views/Components/HeaderRecords.php:

components/forms/records/header-records.blade.php:

<div>
Hello Header Records Heading
{{ $headerTitle }}
</div>
0 likes
5 replies
vincent15000's avatar

You should move the code that generates the title inside the render method and pass the variable to the view.

public function render(): View
{
	$headerTitle = $this->generateTitle();

    return view('components.forms.records.header-records', compact('headerTitle'));
}
1 like
CodezPoet's avatar

Thank you. I tried that solution, but I still get the same error unfortunately.

1 like
CodezPoet's avatar

I found a temporary solution by enclosing the code part in @php tags in the Blade template, instead of the Component Class, however not my preferred solution for various reasons. Than I ran into the same issue for another Component template and class.

What I can find in the documentation is this: https://laravel.com/docs/11.x/blade#component-methods

I copied the method and Component template code, and then I get the same type of error: Undefined variable $isSelected

Wonder what I am overlooking.

adamdavies's avatar

I thnk the problem your having is because your blade syntax is:

<x-forms.records.header-records /> it matches on the view side of things but not the component path. Generally with components the class path matches the view path or mainly the path that you are entering in blade.

So Laravel will be looking for the class component in Views/Components/Forms/Records/HeaderRecords.php and if it cant find it, it will then assume anonymous so look for the blade file alone and then if it cant find that then it will throw exception.

If you match your blade syntax to where the class is and then load the view that you want then the $headerTitle should be defined. It's not defined for before because its not running through the class and just treating it like an anonymous component

Please or to participate in this conversation.