Search code examples
laravel-livewire

Undefined variable $names


The rest of the blade files are reading values from the controller well. The ..layout/app.blade file is getting undefined variable $names from ..Layout/App.php.

Below is my App.php. I have tried to dd($names) seems its the App.php is not being reached.

    <?php

namespace App\Http\Livewire\Layouts;

use Livewire\Component;

class App extends Component
{

    public $names ="Alex Boey";

    public function mount(){
        dd($this->names);
    }
    public function render()
    {
        return view('livewire.layouts.app');
    }
}

app.blade.php

    <head>
    @livewireStyles
</head>
<body>

<div>{{$names}}</div>

{{ $slot }}

@livewireScripts
</body>

View Image to see the files on IDE


Solution

  • A Livewire component can only have a single root element. You're using Livewire to load in the full app view. That simply won't work.

    lostika already provided the answer you're looking for;

    Replace the <div>{{$names}}</div> in your app.blade with <livewire:layouts.app/>, and then inside the view of your Livewire component:

    <div>
        {{$names}}
    </div>
    

    If the app.blade that you posted is the same as your Livewire component's view, then you need to move that to a separate, non-Livewire location.