📖 Scenario: You are creating a reusable alert box component for a Laravel Blade template. This alert box will have a title and a message. You want to use Laravel components and slots to make it easy to reuse and customize the alert box in different parts of your website.
🎯 Goal: Build a Laravel Blade component called alert-box that accepts a title attribute and uses a default slot for the alert message content.
📋 What You'll Learn
Create a Blade component file named alert-box.blade.php with a title attribute
Use a slot to display the alert message inside the component
Use the component in a Blade view passing the title and slot content
Ensure the component renders the title and message correctly
💡 Why This Matters
🌍 Real World
Reusable components with slots help you build consistent UI elements like alerts, modals, and cards in Laravel projects.
💼 Career
Understanding Laravel components and slots is essential for modern Laravel development and improves code reuse and maintainability.
Progress0 / 4 steps
1
Create the Blade component file with a title attribute
Create a Blade component file named alert-box.blade.php inside the resources/views/components directory. Define a $title attribute at the top using @props(['title']). Inside the component, add a <div> with a class alert-box and inside it add an <h3> tag that displays the $title variable.
Laravel
Hint
Use @props(['title']) to declare the title attribute. Use {{ $title }} to display it inside the <h3> tag.
2
Add a slot to display the alert message inside the component
Inside the div with class alert-box, below the h3 tag, add the Blade slot directive {{ $slot }} to display the content passed into the component slot.
Laravel
Hint
Use {{ $slot }} to show the content passed between the component tags.
3
Use the alert-box component in a Blade view with title and slot content
In a Blade view file (e.g., resources/views/welcome.blade.php), use the alert-box component with the syntax <x-alert-box title="Warning">This is an important alert message.</x-alert-box>.
Laravel
Hint
Use the component tag <x-alert-box> with the title attribute and put the alert message between the tags.
4
Add a CSS class to style the alert box
Add a class attribute with value alert-box to the div in the component file alert-box.blade.php. This class will be used for styling the alert box.
Laravel
Hint
Make sure the div has the attribute class="alert-box" for styling.
Practice
(1/5)
1. What is the main purpose of using components in Laravel Blade?
easy
A. To define routes for the application
B. To write raw PHP code inside Blade templates
C. To create reusable pieces of UI that can be included in multiple views
D. To connect Laravel with external databases
Solution
Step 1: Understand what components do
Components in Laravel Blade are designed to hold reusable UI parts, like buttons or cards, so you don't repeat code.
Step 2: Compare options with component purpose
Only To create reusable pieces of UI that can be included in multiple views describes reusable UI pieces. Other options describe unrelated tasks.
Final Answer:
To create reusable pieces of UI that can be included in multiple views -> Option C
Quick Check:
Components = reusable UI [OK]
Hint: Components = reusable UI blocks in Blade templates [OK]
Common Mistakes:
Confusing components with routing or database tasks
Thinking components are for PHP logic only
Assuming components are only for styling
2. Which of the following is the correct syntax to pass a slot named header to a Laravel Blade component called card?
easy
A. Title
B. Title
C.
D. Title
Solution
Step 1: Recall named slot syntax in Blade
Named slots use <x-slot name="slotName">...</x-slot> inside the component tags.
B. The slot name "footer" is reserved and cannot be used
C. Slots must be self-closing tags
D. The unnamed slot is incorrectly declared with <x-slot> instead of using default slot syntax
Solution
Step 1: Check unnamed slot syntax
The default slot content should be placed directly inside the component tags, not wrapped in <x-slot> without a name.
Step 2: Understand correct default slot usage
Using <x-slot> without a name is invalid. Default slot content is just plain content inside the component.
Final Answer:
The unnamed slot is incorrectly declared with <x-slot> instead of using default slot syntax -> Option D
Quick Check:
Default slot = direct content, not tag [OK]
Hint: Default slot is plain content, not wrapped in [OK]
Common Mistakes:
Wrapping default slot content in without name
Thinking all slots must be named
Believing slots must be self-closing
5. You want to create a reusable profile-card component that accepts a header slot for the user's name, a default slot for the bio, and a footer slot for action buttons. Which Blade component template correctly supports these slots?
hard
A.
{{ $slot }}{{ $header }}
B.
{{ $header }}{{ $slot }}
C.
{{ $header }}{{ $footer }}
D.
{{ $footer }}{{ $slot }}
Solution
Step 1: Match slots to variables
The named slots header and footer correspond to $header and $footer. The default slot content is in $slot.
Step 2: Verify correct placement in template
<div class="profile-card">
<header>{{ $header }}</header>
<section>{{ $slot }}</section>
<footer>{{ $footer }}</footer>
</div> places $header in <header>, $slot in <section>, and $footer in <footer>, matching the requirement.