Laravel Blade Layouts: @extends, @section and @yield
You now have two real pages in your invoice app: the invoice list and the invoice detail. Both work, but look at what is missing. Neither page has an <html> tag, a <title>, a navigation menu or a footer. Every page starts with its own <h1> and nothing else.
Adding all of that is easy for one page. But your app will soon have dozens of pages, and each one needs the same header, the same menu and the same footer. If you copy that block into every file, a simple change like adding a Reports link to the menu means editing every single page. You will miss one, and your site will look inconsistent.
Blade solves this with layouts. You write the shared page structure once, and every page reuses it. In this lesson you will learn how to build a master layout, how child pages fill it with @extends, @section and @yield, and how to handle default content, active menu links and more than one layout.
The Problem: Repeating the Page Structure
Here is what the top of your list page would look like if you added the structure by hand:
<!DOCTYPE html>
<html lang="en">
<head>
<title>Invoices</title>
</head>
<body>
<nav>
<a href="{{ route('invoices.index') }}">Invoices</a>
</nav>
<h2>Invoices</h2>
{{-- the real page content --}}
<footer>© 2026 MasterPHP Traders</footer>
</body>
</html>
Now imagine the same 15 lines pasted into the detail page, the create page, the edit page and every future page. Only one part is different on each page, which is the content in the middle. Everything else is a copy. You need a way to say: here is the common frame, and each page only supplies its own middle part.
What Is a Layout?
A layout is a Blade file that holds the shared structure of your pages. It has holes in it, called yields, where each page can place its own content. A child page names the layout it wants, and then fills the holes with sections.
layouts/app.blade.php (head, menu, @yield('content'), footer)
^
| @extends('layouts.app')
|
invoices/index.blade.php fills 'title' and 'content'
invoices/show.blade.php fills 'title' and 'content'
This idea is called template inheritance. The child inherits everything from the layout, and changes only the parts it needs. Layout files usually live in resources/views/layouts. As you learned in the Responses lesson, the view layouts.app means the file resources/views/layouts/app.blade.php.
Creating the Master Layout
The Layout File
Create resources/views/layouts/app.blade.php:
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<title>@yield('title', 'Invoice Manager') | {{ config('app.name') }}</title>
</head>
<body>
<header>
<h1>{{ config('app.name') }}</h1>
<nav>
<a href="{{ route('invoices.index') }}">Invoices</a>
</nav>
</header>
<main>
@session('status')
<p>{{ $value }}</p>
@endsession
@yield('content')
</main>
<footer>
<p>© {{ date('Y') }} MasterPHP Traders</p>
</footer>
</body>
</html>
The config('app.name') call reads the APP_NAME value from your .env file. The flash message block from the Redirects lesson now lives here, so every page that follows a redirect can show it without repeating the code. This layout has no styling yet. Tailwind CSS and Vite are added in the lesson Tailwind CSS & Alpine.js with Vite, and the layout is where those links will go.
How @yield Works
The @yield('content') line is a placeholder. When a page is built, Blade replaces it with whatever the child view put into a section called content. The name is your choice, but content is the common one.
The @yield directive also takes a second argument, which is a default value. In @yield('title', 'Invoice Manager'), the text Invoice Manager is printed if a page does not set its own title.
Creating a Child View
@extends and @section
Now rewrite resources/views/invoices/index.blade.php so that it uses the layout:
@extends('layouts.app')
@section('title', 'Invoices')
@section('content')
<h2>Invoices</h2>
<ul>
@forelse ($invoices as $number => $invoice)
<li>
{{ $loop->iteration }}.
<a href="{{ route('invoices.show', $number) }}">Invoice {{ $number }}</a>
for {{ $invoice['client'] }}
</li>
@empty
<li>No invoices yet. Create your first invoice.</li>
@endforelse
</ul>
@endsection
There are three new directives here:
@extends('layouts.app')says which layout this page uses. Put it at the top of the file.@section('title', 'Invoices')is the short form for a small piece of text. The name and the value go in one line, with no closing directive.@section('content') ... @endsectionis the block form for larger HTML. Everything between the two lines becomes the content.
The controller does not change. It still returns view('invoices.index', [...]), and Laravel applies the layout on its own. Data you pass to the child view is also available inside the layout, which is handy for things like the company name.
A Second Page Using the Same Layout
Here is the detail page from the Blade Basics lesson, now using the layout. Notice that the repeated company heading is gone, because the layout already prints it:
@extends('layouts.app')
@section('title', 'Invoice ' . $invoice['invoice'])
@section('content')
<h2>Invoice {{ $invoice['invoice'] }}</h2>
<p>Client: {{ $invoice['client'] }}</p>
<p>Total: ₹ {{ number_format($invoice['total']) }}</p>
@endsection
The title is built with a PHP expression, so the browser tab for invoice 1001 reads Invoice 1001 | MasterPHP. Open both pages and you will see the same header, menu and footer on each. Now add a link to the menu in layouts/app.blade.php, and it appears on every page at once. That is the whole point of a layout.
Default Content and @parent
Sometimes a layout has an area that pages may change, but that should also have a sensible default. Define it with @section and close it with @show. The directive @show marks the end of the section and also prints it right there. Add a sidebar to the layout:
<aside>
@section('sidebar')
<a href="{{ route('invoices.index') }}">All invoices</a>
@show
</aside>
Pages that do not mention sidebar get the default link. A page that defines the same section replaces the default completely:
@section('sidebar')
<a href="{{ route('invoices.export') }}">Export CSV</a>
@endsection
Here the default link is gone. To keep it and add to it, use @parent, which prints the layout’s version at that point:
@section('sidebar')
@parent
<a href="{{ route('invoices.export') }}">Export CSV</a>
@endsection
The route invoices.export is the one you created in the Responses lesson. Now the list page shows both links, and other pages show only the default one.
Checking Whether a Section Has Content
You can ask Blade if a child page filled a section. This is useful when you do not want an empty box on the page:
@hasSection('actions')
<div class="actions">
@yield('actions')
</div>
@endif
@sectionMissing('actions')
<p>No actions available.</p>
@endif
The @hasSection check is true when the page defined that section. The @sectionMissing check is true when it did not. Both are closed with the ordinary @endif that you know from the last lesson.
Highlighting the Active Menu Link
A menu feels better when the current page is marked. In the Request lesson you used is() on the request, and in the Named Routes lesson you used routeIs(). You can call the same method inside the layout and print a word when it matches:
<nav>
<a href="{{ route('invoices.index') }}"
class="{{ request()->routeIs('invoices.*') ? 'active' : '' }}">
Invoices
</a>
</nav>
The pattern invoices.* matches invoices.index, invoices.show and every other route in the resource, so the link stays marked on all invoice pages. The active class does nothing until you add styles, which you will do with Tailwind later in this chapter.
Using More Than One Layout
An app rarely needs only one frame. The dashboard needs a menu and a sidebar, while the login page should be a clean, centered box. You can create as many layouts as you need, and each page picks one:
resources/views/
layouts/
app.blade.php (menu, header and footer for staff pages)
guest.blade.php (a bare page for login and register)
invoices/
index.blade.php @extends('layouts.app')
show.blade.php @extends('layouts.app')
A layout can also extend another layout. For example, an admin layout could extend layouts.app and add its own sidebar. Do not go deeper than that, because many levels of inheritance make it hard to know where a piece of HTML comes from.
Layouts and Components
Laravel offers a second way to build page frames, using Blade components, which look like <x-layout>. Many new projects prefer it, and you will learn it in the next lesson, Blade Components & Slots. Template inheritance with @extends is still fully supported, and a large number of existing Laravel projects use it. Learning it first makes the component version much easier to follow, because both solve the same problem: a shared frame with a place for each page’s own content.
Common Mistakes
Mistake 1: Wrong Layout Name
@extends('layout.app')
If the folder or file name does not match, Laravel cannot find the layout and stops with:
InvalidArgumentException: View [layout.app] not found.
Check that the folder is layouts, not layout, and that the file is app.blade.php.
Mistake 2: Section Names That Do Not Match
@yield('content') {{-- in the layout --}}
@section('contents') {{-- in the child page --}}
No error appears, but the middle of the page is empty, because the page filled a section that the layout never asked for. The names must be spelled exactly the same.
Mistake 3: Content Outside a Section
@extends('layouts.app')
<h2>Invoices</h2>
@section('content')
...
@endsection
When a view extends a layout, everything outside a @section is ignored. The <h2> above never reaches the browser, and there is no warning. Put all the HTML of the page inside sections.
Mistake 4: Closing an Inline Section with @endsection
@section('title', 'Invoices')
@endsection
The short form with two values is complete in one line. Adding @endsection after it makes Blade look for a section that was never opened:
InvoiceArgumentException: Cannot end a section without first starting one.
Use @endsection only with the block form, where the section has no second value.
Mistake 5: Forgetting @parent
@section('sidebar')
<a href="{{ route('invoices.export') }}">Export CSV</a>
@endsection
The default links from the layout disappear, because this section replaced them. If you wanted to add to the default content, put @parent at the top of the section.
Mistake 6: Using Variables That Only Some Pages Send
<h1>{{ $company }}</h1>
If the layout prints $company, but only the detail page sends it, the list page fails with:
ErrorException: Undefined variable $company
A layout is used by every page, so it must not depend on data that only some controllers pass. Use config() values, a default with ??, or the sharing options you will meet in later lessons.
Quick Summary
- A layout is a Blade file with the shared page structure, usually stored in
resources/views/layouts. @yield('content')marks a place in the layout where a child page puts its own HTML.@yield('title', 'Default')gives a fallback value when a page does not set that section.- A child page starts with
@extends('layouts.app')and fills the holes with@section. - Use
@section('title', 'Invoices')for short text and@section('content') ... @endsectionfor blocks of HTML. - Everything outside a section in a child view is ignored.
- Close a layout section with
@showto give it default content. Inside a child,@parentkeeps that default. @hasSectionand@sectionMissingcheck whether a page filled a section.- Use
request()->routeIs('invoices.*')to mark the active menu link. - You can create several layouts, such as
appandguest, and each page picks one. - Layouts must not depend on data that only some pages send.
What’s Next
Template inheritance works well for the overall frame of a page. But your pages also have smaller pieces that repeat, like an alert box, a button or an invoice card, and these do not fit the idea of a layout. In the next lesson, Blade Components & Slots, you will learn how to turn these pieces into reusable tags such as <x-alert>, and how to pass content into them with slots.