Understanding Laravel Blade Components & Slots

As a long-time WordPress developer transitioning into Laravel, you might find yourself asking: “What exactly are Blade components and slots? And why do they sound so confusing?” Don’t worry — you’re not alone.

This post is designed specifically for WordPress developers to explain Laravel Blade components and slots by comparing them to familiar WordPress concepts, especially shortcodes.


📦 What are Blade Components?

In Laravel, Blade components are reusable UI blocks — just like how in WordPress you’d create a shortcode to insert a button, alert box, or card into your content.

A Blade component consists of:

  • A class file that holds the logic (like your functions.php code for a shortcode).
  • A Blade view file that holds the HTML (like the HTML output inside your shortcode callback).

Example: Alert Box Component

You can generate a new component in Laravel like this:

artisan make:component Alert

This will create:

  • app/View/Components/Alert.php — component logic (like shortcode function).
  • resources/views/components/alert.blade.php — component template (like shortcode HTML).

🕹️ Example: Alert Box (WordPress vs Laravel)

In WordPress (Shortcode)

function wp_custom_alert_shortcode($atts, $content = null) {
    $atts = shortcode_atts(['type' => 'info'], $atts);

    return '<div class="alert alert-' . esc_attr($atts['type']) . '">' . esc_html($content) . '</div>';
}
add_shortcode('custom_alert', 'wp_custom_alert_shortcode');

Usage:

[custom_alert type="warning"]This is a warning![/custom_alert]

Output:

<div class="alert alert-warning">This is a warning!</div>

In Laravel (Blade Component)

app/View/Components/Alert.php

namespace App\View\Components;

use Illuminate\View\Component;

class Alert extends Component
{
    public $type;

    public function __construct($type = 'info')
    {
        $this->type = $type;
    }

    public function render()
    {
        return view('components.alert');
    }
}

resources/views/components/alert.blade.php

<div class="alert alert-{{ $type }}">
    {{ $slot }}  {{-- This is the slot where the content goes --}}
</div>

Usage in Blade Template

<x-alert type="warning">
    This is a warning!
</x-alert>

Output

<div class="alert alert-warning">This is a warning!</div>

✨ Key Point:

The content inside the component tags (This is a warning!) is automatically passed into the default slot ({{ $slot }}).


🧩 What are Slots in Laravel Blade?

Slots are placeholders inside a Blade component where you can inject custom content when using the component. Slots are the Laravel equivalent of the content between WordPress shortcode tags.

  • Default Slot: Main content.
  • Named Slots: Custom sections like headers or footers.

🧩 Example: Card with Header and Footer

WordPress (Nested Shortcodes)

[custom_card]
[header]My Card Header[/header]
This is the main content.
[footer]My Card Footer[/footer]
[/custom_card]

Implementing this in WordPress is complex, requiring nested shortcode parsing, which can break easily.


Laravel (Blade Component with Named Slots)

resources/views/components/card.blade.php

<div class="card">
    <div class="card-header">{{ $header ?? '' }}</div>
    <div class="card-body">{{ $slot }}</div>
    <div class="card-footer">{{ $footer ?? '' }}</div>
</div>

Usage in Blade Template

<x-card>
    <x-slot name="header">My Card Header</x-slot>
    This is the main content.
    <x-slot name="footer">My Card Footer</x-slot>
</x-card>

Output

<div class="card">
    <div class="card-header">My Card Header</div>
    <div class="card-body">This is the main content.</div>
    <div class="card-footer">My Card Footer</div>
</div>

✨ Named slots (like header and footer) make it easy to pass custom sections into your components — no messy parsing required.


🔄 Direct Comparison: WordPress vs Laravel Blade

FeatureWordPress ShortcodeLaravel Blade Component
Reusable BlockShortcodeBlade Component
Data Passed InShortcode AttributesComponent Attributes
Main Content$contentDefault Slot ({{ $slot }})
Multiple SectionsNested Shortcodes (Hard)Named Slots (Easy)
Example[custom_alert type="warning"]Content[/custom_alert]<x-alert type="warning">Content</x-alert>

✅ Why Laravel Blade is Better for Reusable Blocks

IssueWordPress ShortcodesLaravel Blade Components
StructureFunction + HTML mixedClass + Blade View (separate logic & HTML)
AttributesManually handled via shortcode_atts()Passed directly to constructor
Nested SectionsRequires fragile parsingNamed slots are built-in
ReadabilityPHP+HTML mixedClean Blade syntax
ReuseOnly in content editorAnywhere in views, emails, etc.

📣 Final Thoughts

For WordPress developers learning Laravel, Blade components and slots are a game changer. They provide:

  • Cleaner separation of logic and presentation.
  • Simpler handling of nested content.
  • Built-in support for passing data.

Once you understand how slots work, you’ll realize they solve many of the headaches you experienced with shortcodes — especially around nested layouts and content sections.


🧰 Handy Reference

TermMeaning
ComponentReusable UI block (like a shortcode)
AttributeData passed to the component (like shortcode attributes)
Default SlotMain content inside the component
Named SlotCustom content sections like headers or footers