php - Laravel 8.0 Blade nested @extends @section and @yield not working

one text

Solution:

Extending views is for when you call/view a blade file directly, for example via return view('templates.float-buttons.float-buttons'). Sounds like this is not what you are looking for. You'd want to @include the file in your home.blade.php. Make the following changes:

home.blade.php:

@extends('templates.template')
@section('header')
    // some codes here
@endsection

@section('content')
    // some other codes here
    @include('templates.float-buttons.float-buttons')
@endsection

float-buttons.blade.php:

<div>
  My Float Buttons Code
</div>

float-buttons.blade.php will just contain the code for your float buttons


Second part

You can pass parameters to @include when including subviews. See if these changes suit your needs:

home.blade.php

@extends('templates.template')
@section('header')
    // some codes here
@endsection

@section('content')
    // some other codes here
    @include('templates.float-buttons.float-buttons', ['type' => 'anchor'])
@endsection

float-buttons.blade.php:

@if ($type == 'anchor')
<div class="button-float d-flex">
    <span class="fas fa-arrow-up text-lg text-white align-self-center mx-auto"></span>
</div>

@elseif ($type == 'help')
<div class="button-float d-flex">
    <span class="fas fa-question text-lg text-white align-self-center mx-auto"></span>
</div>

@else
{{-- if no type parameter is given --}}
<p>
  Parameter 'type' is required
</p>
@endif

You can then basically use @include('templates.float-buttons.float-buttons', ['type' => 'anchor']) or @include('templates.float-buttons.float-buttons', ['type' => 'help']) wherever you need it. And of course feel free to expand the list


Note: A more elegant solution imho would be to create single blade files for each of your cases ('float-buttons-anchor.blade.php, float-buttons-help.blade.php...) and then include the file you need:@include('templates.float-buttons.float-buttons-help')

Source