为什么我的@yield() 没有显示内容?在 Laravel PHP 模板中

why my @yield() is not showing the content? in Laravel PHP template

你好,我正在尝试学习 laravel,我在使用 @yeild 时在模板中没有显示内容的地方偶然发现了这个问题,它只显示了blade 主文件的内容,不显示 child

中的 @section

下面是我的代码,我做错了什么?我该如何解决这个问题?


views/layouts/sample1_template.blade.php

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <meta http-equiv="X-UA-Compatible" content="ie=edge">
    <title>Layout1</title>
</head>
<body>
    <h1>HEADER</h1>
    
    @yield('contentx')

    <h1>Footer</h1>
</body>
</html>

views/sample1_content.blade.php

@extends('layouts.sample1_template')

@section('contentx')

<h1> Content body </h1>

@endsection

在web.php

Route::get('/sampleLayout', function() {
    return view('layouts.sample1_template');
});

你的问题是你还在 Route 中使用 layouts.sample1_template,而你应该使用 sample1_content

所以,将您的 Route 代码更改为:

Route::get('/sampleLayout', function() {
    return view('sample1_content');
});