agyanadda@gmail.com

Laravel Blade Components

Laravel Basics 1 min read Updated Dec 2, 2026 Topic 10 of 31

Blade components are reusable pieces of UI — alerts, buttons, cards, form inputs — used like HTML tags: <x-alert>.

Anonymous components

Create a file in resources/views/components. The file components/alert.blade.php becomes <x-alert>. A file in a subfolder, components/form/input.blade.php, becomes <x-form.input>.

Props

Declare the attributes your component accepts with @props(['type' => 'info']). Pass them like HTML attributes: <x-alert type="success">. Use : to pass PHP values: :post="$post".

Slots

Whatever you put between the opening and closing tags arrives as {{ $slot }}. Named slots let you fill several areas: <x-slot:title>...</x-slot>.

Extra attributes

{{ $attributes->merge(['class' => 'alert']) }} adds any extra class or id the caller passes.

Class-based components

php artisan make:component Alert creates a PHP class plus a view, useful when the component needs logic (for example, loading data).

Components are also a modern alternative to @extends layouts: <x-layout> ... </x-layout>.

Example

Example — A reusable alert component PHP
{{-- resources/views/components/alert.blade.php --}}
@props(['type' => 'info'])

<div {{ $attributes->merge(['class' => 'alert alert-' . $type]) }}>
    {{ $slot }}
</div>

{{-- Using it in any view --}}
<x-alert type="success">Post saved successfully!</x-alert>

<x-alert type="danger" class="mt-3">Something went wrong.</x-alert>

{{-- Output HTML --}}
{{-- <div class="alert alert-success">Post saved successfully!</div> --}}
{{-- <div class="alert alert-danger mt-3">Something went wrong.</div> --}}

type becomes a prop, the text between the tags becomes $slot, and the extra class="mt-3" is merged into the class list.

Test Yourself With Exercises

Exercise 1 Multiple Choice

Which tag uses resources/views/components/alert.blade.php?

Exercise 2 True / False

Content placed between a component's tags is available as $slot.

Exercise 3 Fill in the Blank

Declare accepted attributes with @___(['type' => 'info']).

Frequently Asked Questions

Use components for reusable UI that takes data or content, such as buttons, cards and inputs: props and slots make them clear. @include is fine for simple partials such as a footer.