MasterPHP.in
Try Compiler →
Laravel Tutorial

Laravel Blade Partials, @include and @stack Explained


Your invoice detail page now looks good, but two small problems are growing. The same summary block, with the invoice number, client name, total and status, is needed on the detail page, and soon on a print page and an email preview. You have copied it once already, and you can feel the third copy coming.

The second problem is a script. To collect payments, the detail page needs Razorpay’s checkout script. If you put that script tag in your layout, every page in the app downloads it, including the invoice list, which never takes a payment. If you put it only in the detail page, it ends up in the wrong place, because the layout controls the bottom of the page, not the child view.

Blade has a simple tool for each problem. Partials with @include let you reuse plain fragments of HTML, and stacks with @push let any page add its own scripts and styles to the layout. In this lesson you will learn both.

The Problem: Repeated Fragments and Page-Specific Assets

Here is the summary block as it exists on the detail page today:

<div class="invoice-summary">
    <h2>Invoice {{ $number }}</h2>
    <p>Client: {{ $invoice['client'] }}</p>
    <p>Total: ₹ {{ number_format($invoice['total']) }}</p>
    <x-status-badge :status="$invoice['status']" />
</div>

Pasting this into the print page means two copies. When the owner asks for the GST amount to appear in the summary, you must edit both and hope you do not forget one.

The script has the opposite problem. A layout cannot know which pages need which scripts. You need a way for a child page to say “please add this to the bottom of the layout,” and that is exactly what stacks do.

 

What Is a Partial?

A partial is a normal Blade file that holds a small fragment of HTML, which you insert into other views. By convention, partials live in resources/views/partials. There is nothing special about the folder, and Laravel treats these files like any other view.

resources/views/
    layouts/
        app.blade.php
    partials/
        invoice-summary.blade.php
    invoices/
        show.blade.php        @include('partials.invoice-summary')
        print.blade.php       @include('partials.invoice-summary')

As in the Layouts lesson, the view name partials.invoice-summary means the file resources/views/partials/invoice-summary.blade.php. A partial has no class and no tag. You insert it with a directive, and it works like pasting the file’s contents at that spot.

 

Creating and Including a Partial

The Partial File

Create resources/views/partials/invoice-summary.blade.php and move the block into it:

<div class="invoice-summary">
    <h2>Invoice {{ $number }}</h2>
    <p>Client: {{ $invoice['client'] }}</p>
    <p>Total: ₹ {{ number_format($invoice['total']) }}</p>
    <x-status-badge :status="$invoice['status']" />
</div>

It is just HTML and Blade. It also uses the <x-status-badge> component from the last lesson, so partials and components work together without any extra steps.

 

Using @include

In the detail page, replace the block with one line:

@extends('layouts.app')

@section('title', 'Invoice ' . $number)

@section('content')
    @include('partials.invoice-summary')
@endsection

When the page renders, Blade replaces the @include line with the contents of the partial. Change the summary once, and every page that includes it changes.

 

Passing Data to a Partial

A partial automatically sees every variable that the parent view has. In the example above, $number and $invoice came from the controller, and the partial could use them without being told. This is convenient, but it hides what the partial needs. Open the file alone and you cannot tell where $invoice comes from.

A clearer habit is to pass the data explicitly as a second argument:

@include('partials.invoice-summary', ['number' => $number, 'invoice' => $invoice])

The array is merged with the parent’s variables, so the partial still sees both. Explicit data has two benefits. The include line documents what the partial expects, and you can pass different values, for example to show a different invoice in a sidebar.

 

Safer Includes: @includeIf, @includeWhen and More

A plain @include throws an error if the view does not exist. Blade has variations for cases where the include is optional or conditional:

{{-- Include only if the view exists --}}
@includeIf('partials.gst-note', ['invoice' => $invoice])

{{-- Include only when a condition is true --}}
@includeWhen($invoice['status'] === 'overdue', 'partials.overdue-warning', ['invoice' => $invoice])

{{-- Include only when a condition is false --}}
@includeUnless($invoice['status'] === 'paid', 'partials.pay-now', ['invoice' => $invoice])

{{-- Include the first view that exists --}}
@includeFirst(['partials.custom-footer', 'partials.footer'], ['year' => 2026])
  • @includeIf skips silently when the view is missing.
  • @includeWhen and @includeUnless replace a small @if block around an @include.
  • @includeFirst takes an array of view names and uses the first one that exists. This is useful for optional overrides.

You can also write the @if yourself. The directives above are only shorter and read more clearly.

 

Looping with @each

A common pattern is to include a partial once for every item in a list. You could write a @foreach with an @include inside, but Blade has a shortcut:

@each('partials.invoice-row', $invoices, 'invoice', 'partials.no-invoices')

The four arguments are:

  • the view to render for each item
  • the array or collection to loop over
  • the variable name the view will receive for each item
  • an optional view to render when the list is empty

The row partial resources/views/partials/invoice-row.blade.php might look like this:

<tr>
    <td>{{ $invoice['client'] }}</td>
    <td>₹ {{ number_format($invoice['total']) }}</td>
</tr>

One thing to remember: views rendered with @each do not inherit the parent’s variables. They receive only the item you named. If the row also needs something else from the page, switch to a normal @foreach with @include and pass the data yourself.

 

Partial or Component?

You now have two tools for reusing HTML, so here is a simple way to choose:

  • Use a partial for a plain fragment that mostly displays data the page already has, such as a summary block, a footer note or a table row.
  • Use a component when the piece takes clear inputs, has its own logic, or accepts content through slots, like the status badge and the invoice card.
  • If a partial starts needing many variables and its own conditions, it is probably a component in disguise.

You will learn a lightweight middle option, anonymous components, in the next lesson. For now, remember that partials are the quickest way to stop copying HTML.

 

Stacks: Letting Pages Add Their Own Scripts and Styles

Now for the second problem. A stack is a named place in your layout where any view can add content. Child pages, partials and components all push into the stack, and the layout prints everything that was pushed.

Defining Stacks in the Layout

Open layouts/app.blade.php and add two stacks, one in the head and one before the closing body tag:

<head>
    <meta charset="UTF-8">
    <title>@yield('title', 'Invoice Manager') | {{ config('app.name') }}</title>

    @stack('styles')
</head>
<body>
    <header>...</header>

    <main>
        @yield('content')
    </main>

    <footer>...</footer>

    @stack('scripts')
</body>

@stack('scripts') is the place where pushed content will appear. Putting scripts at the bottom of the page is a common habit, because the page content can load before the scripts run. The stack name is your choice. styles and scripts are the usual ones.

 

Pushing from a Child Page

In the invoice detail page, add the Razorpay checkout script with @push:

@extends('layouts.app')

@section('title', 'Invoice ' . $number)

@section('content')
    @include('partials.invoice-summary')

    <button id="pay-button">Pay Now</button>
@endsection

@push('scripts')
    <script src="https://checkout.razorpay.com/v1/checkout.js"></script>
@endpush

Now only the detail page loads Razorpay’s script. The invoice list never sees it, because it pushes nothing. How the payment itself works is a topic for later chapters. Here, the point is where the script goes.

Earlier you learned that content outside a @section in a child view is ignored. @push is the exception that makes sense, because it does not print HTML where you write it. It hands content to a stack, and the layout prints it later. So you can place @push after your sections, as shown above.

You can use the same idea for styles that only one page needs:

@push('styles')
    <link rel="stylesheet" href="/css/print-invoice.css">
@endpush

 

Controlling the Order with @prepend

Multiple pushes to the same stack are added in the order they run. Sometimes a script must come first, because other scripts depend on it. Use @prepend to put content at the start of the stack:

@prepend('scripts')
    <script src="/js/helpers.js"></script>
@endprepend

Here is how the final HTML order is decided:

@prepend   ->  placed at the top of the stack
@push      ->  placed after earlier pushes, in the order they ran

You will not need @prepend often. When you find yourself managing the order of many scripts, a build tool is the better answer. You will meet Vite in the Tailwind CSS & Alpine.js lesson later in this chapter.

 

Avoiding Duplicates with @once and @pushOnce

Imagine the Razorpay script is pushed from a payment button partial, and the page shows five invoice cards, each including that partial. The script tag would appear five times. Blade has two directives to prevent that.

@once renders its block only the first time, no matter how often the view is included:

@once
    <p class="notice">All amounts are in Indian rupees.</p>
@endonce

@pushOnce does the same for stacks, so each pushed block is added a single time:

@pushOnce('scripts')
    <script src="https://checkout.razorpay.com/v1/checkout.js"></script>
@endPushOnce

Use @pushOnce inside partials and components, which can be repeated many times on one page. On a normal page that is rendered once, a plain @push is enough.

 

Pushing from Partials and Components

Because the stack belongs to the layout and is printed at the end, any view in the page can push to it. That includes partials and the views of components:

{{-- resources/views/partials/pay-button.blade.php --}}
<button class="pay-button">Pay Now</button>

@pushOnce('scripts')
    <script src="https://checkout.razorpay.com/v1/checkout.js"></script>
@endPushOnce

This keeps the script next to the piece that needs it. Any page that includes the partial gets the script, and pages that do not include it get nothing. The layout needs no change at all.

 

Common Mistakes

Mistake 1: Typing the Wrong View Name

@include('partials.invoice-sumary')

A typo in the name, or a wrong folder, stops the page with:

InvalidArgumentException: View [partials.invoice-sumary] not found.

Check the spelling and the folder. Use @includeIf only when the view is truly optional. Do not use it to hide typos.

 

Mistake 2: A Partial That Needs a Variable Nobody Passed

If the print page includes the summary partial but never sends $invoice, it fails with:

ErrorException: Undefined variable $invoice

The partial worked on the detail page only because the controller sent that variable there. Pass the data explicitly in the @include line, and the problem is easy to see.

 

Mistake 3: Expecting @each to Share the Parent’s Variables

@each('partials.invoice-row', $invoices, 'invoice')

If the row partial also prints $currency from the page, you get an undefined variable error. Views rendered by @each get only the named item. Use @foreach with @include when the partial needs more data.

 

Mistake 4: Pushing to a Stack the Layout Never Prints

@push('script')
    <script src="..."></script>
@endpush

The layout has @stack('scripts'), and the page pushes to script. There is no error. The script simply never appears. Stack names must match exactly, so check the spelling first when a pushed script is missing.

 

Mistake 5: Loading a Script Many Times

A partial that uses @push inside a loop adds its script once for every row. The browser downloads or runs the same script again and again, and the page can behave strangely. Use @pushOnce in partials and components that repeat.

 

Mistake 6: Running Queries Inside a Partial

@php
    $recent = \App\Models\Invoice::latest()->take(5)->get();
@endphp

It looks handy, but when the partial is included inside a loop, the query runs for every row. A partial should display data it receives. Let the controller or a service fetch the data first, then pass it in.

 

Quick Summary

  • A partial is a Blade file for a plain HTML fragment, usually kept in resources/views/partials.
  • Insert a partial with @include('partials.invoice-summary'). The name uses dots for folders.
  • A partial sees the parent’s variables, but passing data explicitly with @include('view', [...]) is clearer.
  • @includeIf, @includeWhen, @includeUnless and @includeFirst handle optional and conditional includes.
  • @each('view', $items, 'item', 'empty.view') renders a partial for every item. It does not share the parent’s variables.
  • Use a partial for simple fragments, and a component when you need inputs, logic or slots.
  • @stack('scripts') in the layout prints content that other views add with @push('scripts').
  • @prepend adds content to the start of a stack.
  • @once and @pushOnce stop repeated pieces from appearing more than once.
  • Partials and components can push to stacks, which keeps a script next to the piece that needs it.
  • Keep partials about display. Fetch data in the controller or a service.

 

What’s Next

Partials are quick, but they have no clear inputs, so they are easy to misuse. Components give you clear inputs, but a full class can feel heavy for a small button or alert. In the next lesson, Anonymous Components & Attribute Bags, you will learn how to build components with only a Blade file, and how to pass extra HTML attributes like class and id through to them.

Frequently Asked Questions

The usual place is resources/views/partials, but Laravel does not require it. Any Blade file inside resources/views can be included, and the dots in the view name match the folders. Many teams also group partials by feature, such as resources/views/invoices/partials, so related files stay together.
Yes. A view loaded with the include directive can use every variable available in the parent view. You can also pass extra data as a second argument. Views rendered with the each directive are different, because they receive only the item from the loop.
The yield directive prints a single section that a child page defines, and a second definition replaces the first. The stack directive prints everything that was pushed to it, so many views can add content and all of it appears. Use yield for page content and titles, and stacks for scripts and styles.
Yes. Stacks belong to the layout, and any view rendered during the page can push to them, including partials and component views. Use pushOnce in views that may appear several times on one page, so the pushed content is not repeated.
Wrap the pushed block in the pushOnce directive, or wrap plain HTML in the once directive. Blade then renders the block only the first time it is reached during that request. This is especially useful for third-party scripts that a reusable partial or component needs.

Share this tutorial