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:
namespace App\View\Components\Forms\Records;
use Closure;
use Illuminate\View\Component;
use Illuminate\Contracts\View\View;
use Illuminate\Support\Facades\Route;
class HeaderRecords extends Component
{
public $headerTitle;
public function __construct()
{
$this->headerTitle = $this->generateTitle();
}
private function generateTitle(): string
{
$segments = explode('.', Route::currentRouteName());
if (isset($segments[1])) {
return ucfirst($segments[1]);
} else {
return 'Records List';
}
}
/**
* Get the view / contents that represent the component.
*/
public function render(): View
{
return view('components.forms.records.header-records');
}
}
components/forms/records/header-records.blade.php:
<div>
Hello Header Records Heading
{{ $headerTitle }}
</div>