@section
Defines a named block of content that a layout renders where it yields that name.
@section('name') ... @endsection @section('name', 'value')Parameters
| Parameter | Type | Description |
|---|---|---|
$name | string | Identifier the layout uses to render this block with @yield. |
$valueoptional | string | Inline content. When supplied as a second argument, the section is defined in one line with no closing directive. |
Return value
Nothing — the block is captured under the given name for a layout to yield; @show yields it immediately.
Errors
An unclosed section produces invalid PHP in the compiled view; yielding a name no section defines simply renders the @yield default or nothing.
@section captures a named block of content. In a child view it fills a placeholder the layout declares with @yield; in a layout it can also define default content of its own.
Closing it: @endsection or @show
These are not interchangeable. @endsection only defines the section — something else must yield it. @show defines it and immediately renders it at that point, which is what you want in a layout that supplies a default sidebar a child may override.
Appending instead of replacing
A child section normally replaces the layout's version. Write @parent inside the child's section to splice the layout's content back in — it is replaced by the parent's content when the view renders, so @parent at the top appends, and at the bottom prepends.
The one-line form
For a short value like a page title, pass it as a second argument: @section('title', 'Page Title'). There is no closing directive in that form, and adding one is a common source of unbalanced-block errors.
What trips people up
Section names are global strings. Two layouts that both use content will happily consume each other's blocks, and a typo in the name fails silently — the section is captured and simply never rendered.
Example
<?php
// resources/views/layouts/app.blade.php — @show defines AND renders
$layout = <<<'BLADE'
<body>
@section('sidebar')
This is the master sidebar.
@show
<div class="container">
@yield('content')
</div>
</body>
BLADE;
// resources/views/child.blade.php — @endsection only defines
$child = <<<'BLADE'
@extends('layouts.app')
@section('title', 'Page Title')
@section('sidebar')
@parent
<p>This is appended to the master sidebar.</p>
@endsection
@section('content')
<p>This is my body content.</p>
@endsection
BLADE;
echo view('child')->render();
// <body>This is the master sidebar.
// <p>This is appended to the master sidebar.</p>
// <div class="container"><p>This is my body content.</p></div></body>When to use it
- Set a per-page title that the layout drops into the <title> tag
- Override a layout's default sidebar on one page only
- Append page-specific scripts to a stack the layout already fills
- Give a layout a default block that most pages inherit unchanged
Frequently asked questions
What is the difference between @endsection and @show in Blade?
@endsection defines the section and stops there, leaving a layout to render it with @yield. @show defines it and immediately yields it at that point, which is how a layout provides default content a child can override.
How do I add to a layout's section instead of replacing it?
Put @parent inside the child's @section block. It is replaced by the layout's version of that section at render time, so @parent first appends your content and @parent last prepends it.
How do I set a page title from a Blade view?
Use the one-line form @section('title', 'Page Title') in the child and @yield('title') in the layout. The inline form takes no closing directive.
More examples
@parent placement decides append or prepend
@parent is a placeholder that is swapped for the layout's version of the section at render time, so its position in the child's block determines the final order. Omitting it entirely replaces the layout's content — which is the default and the reason scripts occasionally vanish from one page after an override is added.
<?php
// Layout: @section('scripts') <script src="/js/app.js"></script> @show
$appends = <<<'BLADE'
@section('scripts')
@parent
<script src="/js/charts.js"></script>
@endsection
BLADE;
// app.js then charts.js
$prepends = <<<'BLADE'
@section('scripts')
<script src="/js/polyfills.js"></script>
@parent
@endsection
BLADE;
// polyfills.js then app.js
$replaces = <<<'BLADE'
@section('scripts')
<script src="/js/standalone.js"></script>
@endsection
BLADE;
// app.js is gone
echo $appends;Detecting whether a section was filled
@hasSection and @sectionMissing let a layout emit wrapper markup only when a child actually supplied the section, so an empty page does not render a stray container div. Both blocks close with @endif, not with a matching @endhasSection — that asymmetry is documented and is a frequent source of confusion.
<?php
// resources/views/layouts/app.blade.php
$layout = <<<'BLADE'
@hasSection('navigation')
<div class="pull-right">
@yield('navigation')
</div>
<div class="clearfix"></div>
@endif
@sectionMissing('navigation')
<div class="pull-right">
@include('default-navigation')
</div>
@endif
BLADE;
echo view('layouts.app')->render();The one-line form
The inline form closes itself. Adding @endsection after it closes whichever section was opened earlier in the file — often the previous one — so the error surfaces as content appearing in the wrong place rather than as a syntax error.
<?php
// Correct: value as the second argument, no closing directive.
$oneLine = <<<'BLADE'
@section('title', 'Page Title')
BLADE;
// Also correct: block form for anything with markup.
$block = <<<'BLADE'
@section('title')
{{ $order->number }} — Orders
@endsection
BLADE;
// Wrong: mixing the two leaves an unclosed block.
$broken = <<<'BLADE'
@section('title', 'Page Title')
@endsection
BLADE;
echo $oneLine;
Discussion