Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

Tasmin's avatar

What and why is @section('content') and @yield('content')

I am new in Laravel. Can someone clarify @yield('content') and @section('content'). I searched on net. But the answer could not satisfy me.

0 likes
4 replies
raminjan's avatar
raminjan
Best Answer
Level 1

Basically yield('content') is a marker. For example, in the tag if you put a yield('content'), your saying this section has the name of content and by the way, you can name inside of the pranthesis anything you want. it doesn't have to be content. it can be yield('inside'). or anything you want.

And then in the child page where you want to import html from your layout page, you just say section('name of the section'). for example, if you have marked your header in your layout page as yield('my_head_band') <-- or anything else you want, then in your child page you just say @section('my_head_band').

This would import the header from the layout page into your child page. vice versa with your body section which in this case was named as content.

Hope this helps.

2 likes
garymize's avatar

@include and @yield are two completely different types of operations to import code into the current file.

@include - import the contents of a separate file into the current file at the location in which it is placed. i.e.:

Layout file:

< some html or other script >

@include('include.file_name') // "include." indicates the subdirectory that the file is in

< more html or other script >
Include File ( a blade file with a block of code ):

< some cool code here >

The contents of 'file_name' ( also a blade file ) is then imported in where the @include directive is located.

@yield imports code from a "section" in the child file ( the "view" blade file. ) i.e.:

Layout file:

< some html or other script >

@yield('needed_section_name')

< more html or other script >

The following section is needed in the "view" blade file that is set to "extend" that layout file.

"View" blade file:

@extends('layout.file_name')
... code as neeeded

@section('needed_section_name')
< some cool code here >
@stop

...
more code as needed

Now the layout file will import in the section of code that matches the naming used.

More on the subject here: https://laravel.com/docs/5.5/blade#defining-a-layout

2 likes

Please or to participate in this conversation.