If you are building modern Laravel applications, understanding Blade Components is extremely important.
Blade Components help you:
-
Reuse UI
-
Write clean templates
-
Reduce duplication
-
Build scalable frontend architecture
This guide explains everything step by step — from beginner to professional level — perfect for publishing on your website.
1️⃣ What Are Blade Components?
Blade Components are reusable UI elements in Laravel.
Instead of repeating this everywhere:
<div class="alert alert-success">
{{ $slot }}
</div>
You can create a reusable component like:
<x-alert type="success">
Profile updated successfully!
</x-alert>
Much cleaner. Much more maintainable.
2️⃣ Why Blade Components Matter
Before components (old way):
-
Lots of copy-paste
-
Hard to maintain
-
UI changes require editing many files
With Blade Components:
-
Centralized UI
-
Clean syntax
-
Professional architecture
-
Easier collaboration with frontend teams
3️⃣ Creating Your First Blade Component
Laravel provides an Artisan command:
php artisan make:component Alert
This creates:
app/View/Components/Alert.php
resources/views/components/alert.blade.php
Now let’s understand both.
4️⃣ Understanding the Component Class
File:
app/View/Components/Alert.php
Example:
namespace App\View\Components;
use Illuminate\View\Component;
class Alert extends Component
{
public $type;
public function __construct($type = 'success')
{
$this->type = $type;
}
public function render()
{
return view('components.alert');
}
}
Explanation:
-
$type→ Public properties become available inside the Blade file. -
__construct()→ Accepts attributes from component usage. -
render()→ Returns the Blade view.
5️⃣ Understanding the Blade View
File:
resources/views/components/alert.blade.php
Example:
<div class="alert alert-{{ $type }}">
{{ $slot }}
</div>
What is $slot?
$slot contains the content placed inside the component.
Example:
<x-alert type="danger">
Something went wrong!
</x-alert>
Here:
-
type="danger"→ Passed to constructor -
Text inside → Stored in
$slot
6️⃣ How to Use Blade Components
Inside any Blade file:
<x-alert type="success">
Profile updated!
</x-alert>
Laravel automatically finds:
resources/views/components/alert.blade.php
7️⃣ Passing Multiple Attributes
Example:
public $type;
public $message;
public function __construct($type, $message)
{
$this->type = $type;
$this->message = $message;
}
Usage:
<x-alert type="warning" message="Check your input!" />
Blade:
<div class="alert alert-{{ $type }}">
{{ $message }}
</div>
8️⃣ Anonymous Components (No Class Needed)
If you only need UI (no logic), you can skip the class.
Create:
resources/views/components/button.blade.php
<button class="btn btn-primary">
{{ $slot }}
</button>
Use it:
<x-button>
Save
</x-button>
This is called an Anonymous Component.
9️⃣ Default Attribute Values
You can define defaults inside constructor:
public function __construct($type = 'info')
Or inside Blade:
@props(['type' => 'info'])
Example:
@props(['type' => 'primary'])
<button class="btn btn-{{ $type }}">
{{ $slot }}
</button>
🔟 Merging Attributes (Very Important)
This is powerful and professional.
Example:
<button {{ $attributes->merge(['class' => 'btn btn-primary']) }}>
{{ $slot }}
</button>
Usage:
<x-button class="mt-3">
Submit
</x-button>
Output:
<button class="btn btn-primary mt-3">
This keeps your component flexible.
1️⃣1️⃣ Named Slots
Sometimes you need multiple content sections.
Component:
<div class="card">
<div class="card-header">
{{ $header }}
</div>
<div class="card-body">
{{ $slot }}
</div>
</div>
Usage:
<x-card>
<x-slot name="header">
User Information
</x-slot>
This is body content.
</x-card>
Named slots make components powerful and clean.
1️⃣2️⃣ Component Namespacing (Advanced)
You can organize components:
resources/views/components/admin/button.blade.php
Usage:
<x-admin.button>
Delete
</x-admin.button>
This helps structure large applications.
1️⃣3️⃣ Inline Components
Laravel can generate inline components:
php artisan make:component Badge --inline
The view is embedded inside the class file.
Best for small components.
1️⃣4️⃣ Dynamic Components
You can dynamically render components:
<x-dynamic-component :component="$componentName" />
Useful for:
-
Theming systems
-
Role-based UI
-
Dynamic layouts
1️⃣5️⃣ Real-World Example: Building a Reusable Card Component
Create:
php artisan make:component Card
Blade:
<div {{ $attributes->merge(['class' => 'card shadow-sm']) }}>
@isset($title)
<div class="card-header">
{{ $title }}
</div>
@endisset
<div class="card-body">
{{ $slot }}
</div>
</div>
Usage:
<x-card class="mb-4">
<x-slot name="title">
Dashboard
</x-slot>
Welcome back, user!
</x-card>
Clean. Reusable. Professional.
1️⃣6️⃣ Blade Components vs Includes
| Blade Include | Blade Component |
|---|---|
@include() | <x-component /> |
| Simple partial | Structured UI |
| No attribute merging | Attribute merging |
| No slots | Named slots |
| Limited flexibility | Highly reusable |
For modern applications → Components are better.
1️⃣7️⃣ Best Practices (Professional Level)
✅ Keep components small and focused
✅ Use anonymous components for UI-only
✅ Use class-based components when logic is needed
✅ Use attribute merging for flexibility
✅ Organize components into folders
✅ Use consistent naming conventions
1️⃣8️⃣ When Should You Use Blade Components?
Use them for:
-
Buttons
-
Alerts
-
Cards
-
Modals
-
Form inputs
-
Layout sections
-
Navigation menus
Basically → any reusable UI block.
🎯 Final Summary
Blade Components allow you to:
-
Write cleaner Blade templates
-
Avoid duplication
-
Build scalable frontend architecture
-
Create reusable UI systems
-
Structure large Laravel applications professionally
If you are building serious Laravel applications, mastering Blade Components is not optional — it is essential.
