skip to Main Content

My goal is to have my layout.blade.php to automatically @yield the header.blade.php when the body view is accessed through the route.

UPDATE

I also want to avoid defining the header view in the body view as it may grow with multiple different contents in different view files. In future when I need to change to different header view, I only need to change it at the layout file, e.g. @yield("header2").


The following are the files:

Stored in resources/views/layouts/layout.blade.php

<body>
    @yield('header')
    @yield('body')
</body>

Stored in resources/views/headers/header.blade.php

@extends('layouts.layout')

@section('header')
<h1>
    My Header
</h1>
@endsection

Stored in resources/views/body.blade.php

@extends('layouts.layout')

@section('body')
<div class="content">
   This is body!
</div>
@endsection

Stored in routes/web.php

use IlluminateSupportFacadesRoute;

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

But when accessing http://127.0.0.1:8000/body, my header is not appearing, why? According to Bing AI Chatbot, she is able to render the header by following the above structure, I wonder why I can’t. Any help will be appreciated, thanks!

3

Answers


  1. Try with this:

    In your body.blade.php file:

    @extends('layouts.layout')
    
    @section('header')
        @include('headers.header')
    @endsection
    
    @section('body')
    <div class="content">
       This is body!
    </div>
    @endsection
    

    In your header.blade.php file:

    @section('header')
    <h1>
        My Header
    </h1>
    @endsection
    
    Login or Signup to reply.
  2. The reason your header is not appearing is because you are extending the layout.blade.php file in both the header.blade.php and body.blade.php views. This causes the layout.blade.php file to be included twice in the final rendered output, resulting in the header being overwritten or not appearing at all.

    To fix this issue, you should remove the @extends(‘layouts.layout’) directive from the header.blade.php view since it is unnecessary. The header.blade.php view should only contain the content for the header itself.

    Try This:

    layout.blade.php:

    <body>
        @yield('header')
        @yield('body')
    </body>
    

    header.blade.php:

    <h1>
        My Header
    </h1>
    

    body.blade.php:

    @extends('layouts.layout')
    
    @section('body')
     <div class="content">
       This is body!
     </div>
    @endsection
    
    Login or Signup to reply.
  3. This is because you are not including your header in your body blade file:

    // Stored in resources/views/body.blade.php
    
    @extends('layouts.layout')
    @section('header')
        @include('headers.header')
    @endsection
    
    @section('body')
        <div class="content">
            This is body!
        </div>
    @endsection
    

    Also, you should remove @extends(‘layouts.layout’) from header file.

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search