3

I am using livewire 2.2 with laravel 7.24 in my laravel project. When i load component using

Route::get('/path', ComponentClassPath);

it works, but when i load my livewire component inside a blade view. For instance, test.blade.php like this:

@extends('layouts.app')

@section('content')
    @livewire("my-livewire-component")
@endsection

it doesn't work and shows the below error:

Undefined variable: slot (View: E:\xampp\htdocs\lw\resources\views\layouts\app.blade.php) .

app.blade.php

<head>
   @livewireStyles
</head>
<body>
   <div class="container">
       {{ $slot }}
   </div>

   @livewireScripts
</body>

Any solution ?

1
  • Can you share your ComponentClassPath too Commented Sep 27, 2020 at 11:59

4 Answers 4

14

Since you're using the @section directive (where you @extend a layout, and use sections), and not the component $slot approach (see https://laravel.com/docs/8.x/blade#slots), you should be using @yield instead of echoing the "special" $slot variable.

Alternatively you could go with the slots-approach, but you would generally choose one or the other. Going with that approach means a different structure to your blade files, and there's nothing wrong with either way -- just be consistent about which one you choose.

To continue using sections and extending layouts, simply replace {{ $slot }} with @yield('content'), like

<div class="container">
    @yield('content')
</div>
Sign up to request clarification or add additional context in comments.

4 Comments

I followed a video tutorial in which they used both at same time. I think that's in version 1.x. Right?
If i replace $slot with yield('content'), the error goes and the blade files are rendered well. But the issue is the component rendered directly by livewire (not blade) has nothing in it, just empty.
@MUHAMMADSiyab me too
In your Livewire component, hope you are rendering this way: return view('livewire.componentName')->extends('layouts.app');
2

instead of

@extends('layouts.app')

I found that laravel 10 uses x-app-layout

<x-app-layout>
Some content here...
</x-app-layout>

Comments

1

If you still want to use slot please create component php artisan make:component Layouts/Login and on render function in Livewire controller Http/Livewire/... add layout

public function render()
    {
        return view('livewire.pages.login')
            ->layout('components.layouts.login');
    }

Comments

0

This Error is occurred because in your normal page livewire slot variable is not found so here is the solution you may try with your code.

@if (!empty($slot))
  {{ $slot }}
@else
  @yield('content')
@endif

In my case i wanted to load content if slot is not found.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.