Php 如何通过Laravel8中的控制器将页面标题传递给视图?

Php 如何通过Laravel8中的控制器将页面标题传递给视图?,php,laravel-8,laravel-controller,Php,Laravel 8,Laravel Controller,我试图通过控制器动态地将每个页面标题传递给视图。我已将页眉包含在views/components文件夹中,并将其作为传递给每个视图。然后在header标记中,我试图将页面标题作为{{$title}}进行回显,但是我得到了错误“Undefined variable$title”。如何调试此错误 控制器 public function index() { $title = "Home || My site"; return view('home', compact

我试图通过控制器动态地将每个页面标题传递给视图。我已将页眉包含在views/components文件夹中,并将其作为
传递给每个视图。然后在header标记中,我试图将页面标题作为{{$title}}进行回显,但是我得到了错误“Undefined variable$title”。如何调试此错误

控制器

public function index() {
    $title = "Home || My site";
    return view('home', compact('title'));
}
components/header中的header.blade.php文件

<head>     
    <title>
        {{ $title }}
    </title>
</head>

{{$title}}
顶部包含标题的home.blade.php视图

<x-header></x-header>
<div class="search-relative search-relatives " id="search-relatives">
<div class="item height25vh" id="vid">

正确设置组件属性

<x-header :title=$title></x-header>

我假设您已经设置了header.php文件

试试:

<x-header>{{ $title ?: 'Default Value' }}</x-header>
{{$title?:'Default Value'}

成功了!!!谢谢