Knowledge Seeker
Knowledge Seeker

Reputation: 566

@yield, @section in Laravel

Hello everyone I want to load shopReq.blade.php file in master.blade.php and I am successful in doing this. But when I load modals.blade.php file in shopReq.blade.php it does not included. Am I doing something wrong here? Please help.

master.blade.php (in views/layouts):

<html>
   <body >
          @yield('content')
    </body>
</html>

shopReq.blade.php (in views)

@extends('layouts.master')
@section('content')
<h1>Hello I am in shopReq.blade.php
@yield(content)
@endsection

modals.blade.php (in views)

@extends('shopReq')
@section('content')
<h1>Hello I am in modals.blade.php
@endsection

web.php:

Route::group(['middleware' =>['web']], function()
    {
    Route::get('/', function () {
        return view('welcome');
    })->name('home');
});

Upvotes: 3

Views: 4875

Answers (3)

bhill77
bhill77

Reputation: 875

replace @yield(content) on shopReq.blade.php with @include('modals')

modals.blade.php

<h1>Hello I am in modals.blade.php</h1>

on routes

return view('shopReq');

Upvotes: 3

lewis4u
lewis4u

Reputation: 15037

As the user milo526 has said.

You have @yield('content') twice.

For any blade directive (yield, section, component...)

if there is a same name inside brackets it will be overwritten.

And now I have spot that you don't have the quotes ' ' on your second yield:

@yield(content)

Upvotes: 2

milo526
milo526

Reputation: 5083

The problem is with the similar name. You can't yield('content') inside a section('content') you will have to rename one of the sets.

So either change the yield in master and section in shopReq or the yield in shopReq en the section in modal.

Upvotes: 1

Related Questions