Laravel Blade Components and Slots Explained
In the last lesson you moved the page frame into a layout, so the header, menu and footer now live in one place. But your pages still repeat smaller pieces. The Paid, Pending and Overdue badge from the list page is also needed on the detail page, and soon on a dashboard and a client page. Each time you copy the same @if chain, and each time you risk changing it in one place and forgetting another.
A layout cannot help here. A layout is the frame of a whole page, while a badge is a small piece inside it. What you need is a reusable piece that you write once and place anywhere with a single tag.
Blade calls these pieces components. In this lesson you will learn how to create a component with Artisan, pass data into it, and pass blocks of HTML into it with slots.
The Problem: Copy-Pasted Pieces
This is the badge block from the Control Structures lesson:
@if ($invoice['status'] === 'paid')
<strong>Paid</strong>
@elseif ($invoice['status'] === 'overdue')
<strong>Overdue</strong>
@else
<strong>Pending</strong>
@endif
Now imagine it pasted into four pages. The owner says the word Overdue should be shown as Payment Overdue, and the badge needs a CSS class. You must find and change every copy. A component fixes this, because the badge lives in one file, and every page only contains a short tag like this:
<x-status-badge status="paid" />
What Is a Blade Component?
A component is a reusable piece of a page with its own Blade view. You use it like an HTML tag that starts with x-. There are two kinds:
- Class-based components have a PHP class and a Blade view. You will build these in this lesson.
- Anonymous components have only a Blade view and no class. You will learn them in the Anonymous Components & Attribute Bags lesson.
Components are not the only way to reuse HTML. The @include directive is a simpler tool for plain fragments, and the next lesson covers it. A component is the better choice when the piece takes data, has its own logic, or accepts content from the page that uses it.
Creating Your First Component
Generating a Component with Artisan
Use the make:component command. The name is written in PascalCase, like a class name:
php artisan make:component StatusBadge
Laravel creates two files:
app/View/Components/StatusBadge.php resources/views/components/status-badge.blade.php
The class holds the data and logic, and the view holds the HTML. The class name StatusBadge becomes the tag <x-status-badge>, with every capital letter turned into a lowercase word separated by a dash.
The Component Class
Open app/View/Components/StatusBadge.php. Laravel already wrote the basic structure. Replace its contents with this:
<?php
namespace App\View\Components;
use Closure;
use Illuminate\Contracts\View\View;
use Illuminate\View\Component;
class StatusBadge extends Component
{
public function __construct(public string $status = 'pending')
{
}
public function label(): string
{
return match ($this->status) {
'paid' => 'Paid',
'overdue' => 'Overdue',
default => 'Pending',
};
}
public function render(): View|Closure|string
{
return view('components.status-badge');
}
}
The constructor receives the data the component needs. Writing public in front of the argument also creates a property of the same name. The label() method turns the status into the text to show, using PHP’s match, and the render() method says which Blade file to use.
The Component View
Now write the view, resources/views/components/status-badge.blade.php:
<span class="badge badge-{{ $status }}">{{ $label() }}</span>
Every public property of the class is available as a variable in the view, so $status works without any extra code. Every public method is available too, and you call it like a function, as in $label(). Protected and private members are not visible to the view.
Using the Component
Place the component with its tag. Use the real status from your data:
<x-status-badge :status="$invoice['status']" />
For a paid invoice, the browser receives:
<span class="badge badge-paid">Paid</span>
Replace the @if chain on the list page with this one line, and do the same on the detail page. If the owner wants Overdue to say Payment Overdue, you change one file.
Passing Data into Components
Static Values and PHP Expressions
There are two ways to pass data in a tag, and the difference is a single colon:
<x-status-badge status="paid" /> <x-status-badge :status="$invoice['status']" />
Without a colon, the value is plain text, so the first line passes the word paid. With a colon, the value is a PHP expression, so the second line passes whatever $invoice['status'] holds. Use the colon for variables, numbers, arrays and anything calculated.
Attribute names in a tag use kebab-case, while the constructor uses camelCase. An argument called $showGst in the class is written as show-gst in the tag:
<x-invoice.card :show-gst="true" />
Default Values
In the class above, $status = 'pending' gives the argument a default. So <x-status-badge /> with no status still works and shows Pending. Use defaults for options that are not always needed. Leave them out for data that must be supplied, so that PHP stops with a clear error if something is missing.
Slots: Passing HTML into a Component
Props are good for small values such as a status or a number. When you want to pass a block of HTML into a component, use a slot. A slot is the content you write between the opening and closing tags.
The Default Slot
Create an invoice card component. This time, place it in a folder:
php artisan make:component Invoice/Card
The folder becomes part of the tag name with a dot, so this component is used as <x-invoice.card>. The files are app/View/Components/Invoice/Card.php and resources/views/components/invoice/card.blade.php. Update the constructor in the class:
public function __construct(
public int $number,
public array $invoice,
) {
}
The render() method should return view('components.invoice.card'). Now write the view:
<div class="invoice-card">
<h3>Invoice {{ $number }}</h3>
<p>{{ $invoice['client'] }}</p>
<div class="invoice-card-body">
{{ $slot }}
</div>
</div>
The {{ $slot }} line is where the content from the page appears. Use the card like this:
<x-invoice.card :number="$number" :invoice="$invoice">
<x-status-badge :status="$invoice['status']" />
<p>Total: ₹ {{ number_format($invoice['total']) }}</p>
</x-invoice.card>
Everything between the tags goes into $slot, and a component can sit inside a slot, as the badge does here. Variables used inside the slot, like $invoice, come from the page that uses the card, not from the component. Values printed with {{ }} inside a slot are still escaped, so the XSS protection you learned in the Blade Basics lesson still applies.
Named Slots
Sometimes a component has more than one place for content. The card may need a heading area and a footer with buttons. Add named slots to the card view:
<div class="invoice-card">
<h3>{{ $heading }}</h3>
<div class="invoice-card-body">
{{ $slot }}
</div>
@isset($footer)
<div class="invoice-card-footer">
{{ $footer }}
</div>
@endisset
</div>
On the page, fill each named slot with the <x-slot:name> tag. Anything outside a named slot goes to the default $slot:
<x-invoice.card :number="$number" :invoice="$invoice">
<x-slot:heading>
Invoice {{ $number }} for {{ $invoice['client'] }}
</x-slot:heading>
<x-status-badge :status="$invoice['status']" />
<x-slot:footer>
<a href="{{ route('invoices.show', $number) }}">View details</a>
</x-slot:footer>
</x-invoice.card>
You may also see the older form, <x-slot name="footer">, which works the same way. The @isset check hides the footer area when a page does not provide one. For the default slot, you can use $slot->isEmpty() for the same purpose.
Organizing Components in Folders
As your app grows, a flat list of components becomes hard to scan. Folders keep related components together, and the dot in the tag follows the folder path:
app/View/Components/
Invoice/
Card.php <x-invoice.card>
StatusBadge.php <x-status-badge>
resources/views/components/
invoice/
card.blade.php
status-badge.blade.php
Laravel finds class-based components inside app/View/Components by itself, so you do not need to register anything. This is the same convention-over-configuration idea you saw in the Features of Laravel lesson.
Components as Page Layouts
Components can also act as page layouts. Instead of @extends, a page is wrapped in a component tag, and the page content goes into {{ $slot }}. Create the file resources/views/components/layouts/app.blade.php with no class:
<!DOCTYPE html>
<html lang="en">
<head>
<title>{{ $title ?? 'Invoice Manager' }} | {{ config('app.name') }}</title>
</head>
<body>
<main>
{{ $slot }}
</main>
</body>
</html>
Then use it in a page:
<x-layouts.app>
<x-slot:title>Invoices</x-slot:title>
<h2>Invoices</h2>
</x-layouts.app>
A component with no class is an anonymous component, and the next lessons explain how they work in detail. Both styles are valid in Laravel 13. This course continues with @extends for page frames, and uses components for the smaller pieces inside the pages.
Putting It Together: The Invoice List
Here is the content of the list page after this lesson. The badge and the card are now components, and the page itself only decides what to show:
@extends('layouts.app')
@section('title', 'Invoices')
@section('content')
<h2>Invoices</h2>
@forelse ($invoices as $number => $invoice)
<x-invoice.card :number="$number" :invoice="$invoice">
<x-slot:heading>
Invoice {{ $number }}
</x-slot:heading>
<x-status-badge :status="$invoice['status']" />
<x-slot:footer>
<a href="{{ route('invoices.show', $number) }}">View details</a>
</x-slot:footer>
</x-invoice.card>
@empty
<p>No invoices yet. Create your first invoice.</p>
@endforelse
@endsection
For the invoice 1001, the browser receives HTML like this:
<div class="invoice-card">
<h3>Invoice 1001</h3>
<div class="invoice-card-body">
<span class="badge badge-paid">Paid</span>
</div>
<div class="invoice-card-footer">
<a href="/invoices/1001">View details</a>
</div>
</div>
The card also prints the client name, which this short view leaves out. The final design, with Tailwind CSS, comes in the lesson on Building a Reusable UI Kit, where you will also build the navbar and alert components.
Common Mistakes
Mistake 1: Forgetting the Colon
<x-invoice.card number="$number" :invoice="$invoice" />
Without the colon, Laravel passes the text $number as a string. Because the class expects an integer, PHP stops with:
TypeError: App\View\Components\Invoice\Card::__construct(): Argument #1 ($number) must be of type int, string given
When the argument is a string, like status, there is no error at all. The badge simply shows Pending, because the text $invoice['status'] matches nothing. If a component shows wrong data and no error, check the colon first.
Mistake 2: Wrong Tag Name
<x-invoice-card :number="$number" :invoice="$invoice" />
A component in a folder uses a dot, not a dash. Laravel cannot find a component with this name, and says:
InvalidArgumentException: Unable to locate a class or view for component [invoice-card].
Match the tag to the path. Invoice/Card is x-invoice.card, and StatusBadge is x-status-badge.
Mistake 3: Forgetting {{ $slot }}
If the component view has no {{ $slot }}, everything you write between the tags is thrown away. There is no error, and the content just does not appear. Check the component view if the middle of a component is empty.
Mistake 4: Data That the View Cannot See
public function __construct(string $status = 'pending')
Without public, the argument is only a local variable of the constructor, and the view cannot use it:
ErrorException: Undefined variable $status
The component view sees only the public properties and methods of the class, the slots, and the attributes. It does not see the variables of the page that uses it, so pass everything through the tag.
Mistake 5: Using a Named Slot That Was Not Passed
{{ $footer }}
If a page does not provide the footer slot, the variable does not exist, and Laravel throws Undefined variable $footer. Wrap optional slots in @isset($footer), as shown earlier.
Mistake 6: Querying the Database Inside a Component
public function __construct(public int $clientId)
{
$this->client = \App\Models\Client::find($clientId);
}
It looks tidy, but a card used in a list of 50 invoices now runs 50 queries, which is the N+1 problem you will meet in the Eager Loading lesson. A component should display data it receives. Let the controller or service fetch everything first, and pass it in.
Quick Summary
- A Blade component is a reusable piece of a page, used with a tag that starts with
x-. - Create one with
php artisan make:component StatusBadge, which makes a class inapp/View/Componentsand a view inresources/views/components. - The class name
StatusBadgebecomes the tag<x-status-badge>, and a folder likeInvoice/Cardbecomes<x-invoice.card>. - Public properties and public methods of the class are available in the view, and methods are called like
$label(). - Pass plain text as
status="paid"and PHP expressions with a colon, as in:status="$invoice['status']". - Tag attributes use kebab-case, and constructor arguments use camelCase.
- Content between the tags goes to
{{ $slot }}, and extra areas use named slots such as<x-slot:footer>. - Check optional slots with
@isset($footer)or$slot->isEmpty(). - A component only sees what you pass to it, not the variables of the parent page.
- Components can also act as layouts with
<x-layouts.app>, but this course keeps@extendsfor page frames. - Keep components about display. Fetch data in the controller or service.
What’s Next
Components are powerful, but not every repeated piece of HTML needs a class, props and slots. Sometimes you only want to reuse a plain fragment, such as a block of form fields or a list of scripts that only one page needs. In the next lesson, Partials, @include & @stack, you will learn the simpler tools for these cases, and how pages can add their own scripts to the layout.