Kritim Yantra
Jun 15, 2025
Imagine you’re assembling a Lego set. Instead of gluing pieces together permanently, you snap them in and out as needed. That’s exactly how Laravel slots work—they let you build dynamic, reusable UI components that you can customize on the fly!
Whether you're creating alerts, modals, cards, or complex layouts, slots help you:
✔ Avoid copy-pasting code (DRY principle)
✔ Make components flexible (swap content easily)
✔ Keep your project organized (cleaner Blade templates)
In this guide, we’ll break down exactly how slots work with real-world examples, so you can start using them like a Laravel pro.
A slot is a placeholder inside a component where you can inject content later.
Run:
php artisan make:component Alert
This generates:
app/View/Components/Alert.php
(logic) resources/views/components/alert.blade.php
(template)<!-- resources/views/components/alert.blade.php -->
<div class="alert alert-{{ $type }}">
{{ $slot }} <!-- This is where dynamic content goes! -->
</div>
<x-alert type="success">
🎉 <strong>Success!</strong> Your data was saved.
</x-alert>
<x-alert type="error">
❌ <strong>Error!</strong> Please try again.
</x-alert>
Output:
✅ A styled success alert
❌ A styled error alert
No duplicate code! Just one component, multiple uses.
What if you need more than one customizable section? Use named slots!
card.blade.php
)<div class="card">
<div class="card-header">
{{ $header }} <!-- Named slot -->
</div>
<div class="card-body">
{{ $slot }} <!-- Default slot (main content) -->
</div>
<div class="card-footer">
{{ $footer }} <!-- Named slot -->
</div>
</div>
<x-card>
<x-slot name="header">
<h3>Welcome, User!</h3>
</x-slot>
<p>This is the main content of your card. Add anything here!</p>
<x-slot name="footer">
<button>Like</button>
<button>Share</button>
</x-slot>
</x-card>
Result:
📦 A fully customizable card with header, body, and footer sections!
<!-- modal.blade.php -->
<div class="modal">
<h2>{{ $title }}</h2>
<div class="modal-body">{{ $slot }}</div>
<div class="modal-actions">{{ $actions }}</div>
</div>
Usage:
<x-modal>
<x-slot name="title">Delete This File?</x-slot>
<p>Are you sure? This action cannot be undone.</p>
<x-slot name="actions">
<button>Cancel</button>
<button class="danger">Delete</button>
</x-slot>
</x-modal>
<!-- layout.blade.php -->
<div class="page">
@if(isset($sidebar))
<aside>{{ $sidebar }}</aside>
@endif
<main>{{ $slot }}</main>
@if(isset($footer))
<footer>{{ $footer }}</footer>
@endif
</div>
Usage:
<x-layout>
<x-slot name="sidebar">
<nav>Menu Links Here</nav>
</x-slot>
<h1>Welcome to My Website!</h1>
<p>Main content goes here...</p>
</x-layout>
<!-- profile.blade.php -->
<div class="profile-card">
<div class="avatar">{{ $avatar }}</div>
<div class="details">{{ $slot }}</div>
<div class="bio">{{ $bio ?? 'No bio yet.' }}</div>
</div>
Usage:
<x-profile>
<x-slot name="avatar">
<img src="user.jpg" alt="Profile Pic">
</x-slot>
<h3>John Doe</h3>
<p>Web Developer</p>
<x-slot name="bio">
I love coding in Laravel and building cool stuff!
</x-slot>
</x-profile>
{{ $slot ?? 'Fallback text' }}
if content is optional. ✔ Slots = Placeholders for dynamic content inside components.
✔ Default slot ($slot
) holds the main content.
✔ Named slots (<x-slot name="...">
) allow multiple dynamic sections.
✔ Use cases: Alerts, modals, cards, layouts, profile widgets, and more!
Now that you understand slots, try:
Slots make your Laravel components 10x more powerful. Start using them today!
💬 Your Turn!
Have you used slots in Laravel? What cool components have you built? Let’s discuss in the comments! 👇
No comments yet. Be the first to comment!
Please log in to post a comment:
Sign in with Google