Can't pass title to Livewire route

What seems to be the problem: I’m trying to pass params to a layout file as described here:
https://laravel-livewire.com/docs/rendering-components. It seems that no matter what I do I cannot pass a “title” into my layout to render as the title section.

Steps to Reproduce:

Here is my route:

    Route::livewire('sets', 'sets-list-live-component')
        ->layout('layouts.app', ['title' => 'Card Sets'])
        ->name('sets');

Here is my layout:

@extends('layouts.base')

@section('body')
    <x-navbar/>
    @hasSection('title')
        <x-header>
            @yield('title')
        </x-header>
    @endif

    <main>
        <x-container>
            @yield('content')
        </x-container>
    </main>
@endsection

Are you using the latest version of Livewire: yes (1.3.1)

Do you have any screenshots or code examples: see above

Instead of doing that, the best practice of passing the title into your main layouts is to make a @yield directive in the title tag, it’s better.

For example:

In your layouts.app you can do it like so

...
{{-- You can define default value here is you do no pass anything into the title section @yield('title', 'Default') --}}

<title>env('app.name') | @yield('title')</title>
...

And in your view, let say you have the home page

@extends('layouts.app')
@section('title', 'Home')

@section('content')
  ...
   Your Content here
  ...
@endsection

General Notes

  • if you want to use a global variable like settings for example you can register it globally in you AppServiceProvider with view composer

  • if you want to pass some data into the view, it better to pass it into the controller

  • You can’t share the data in layouts function but you can in Livewire method itself Route::livewire('sets', ['title' => 'Card Sets'])

Hope that makes sense to you.

1 Like

@skywalker thanks for the response. So your response made me realize my problem. I was trying to yield a “title”, but in reality want I wanted was a “header-title” or something like that. I didn’t realize until just now that I was causing a conflict with a section I already had configured in the base layout…

Thanks for the tips though and the detailed response.

I’m Glade That Helps you and Your Are welcome any time.