Blade layouts and components help you reuse common parts of your website, such as headers, navigation bars, sidebars, and footers. They reduce repeated code and make your Laravel application easier to maintain.
A Blade layout is a reusable page structure that multiple views can extend. It can contain the HTML document, navigation, footer, and common CSS and JavaScript files.
For example, create this file:
resources/views/layouts/app.blade.php
Inside app.blade.php:
<!DOCTYPE html>
<html>
<head>
<title>Soopro Pathshala</title>
</head>
<body>
<header>
<h1>Soopro Pathshala</h1>
</header>
<main>
@yield('content')
</main>
<footer>
<p>Copyright 2026 Soopro Pathshala</p>
</footer>
</body>
</html>
The @yield('content') directive defines
a section where child views can insert their content.
Use @extends to tell a Blade view which
layout it should use.
Create resources/views/home.blade.php.
@extends('layouts.app')
@section('content')
<h2>Welcome to Laravel</h2>
<p>This is the home page.</p>
@endsection
Laravel uses the layout from
layouts/app.blade.php and inserts
the content into @yield('content').
The layout's header and footer are reused without repeating their HTML in every page.
A layout can contain multiple named sections.
Inside app.blade.php:
<head>
<title>@yield('title', 'Soopro Pathshala')</title>
</head>
<body>
<header>Website Header</header>
<main>
@yield('content')
</main>
<footer>Website Footer</footer>
</body>
Inside about.blade.php:
@extends('layouts.app')
@section('title', 'About Us')
@section('content')
<h2>About Us</h2>
<p>Learn about our training programs.</p>
@endsection
The title section changes the page title, while the content section provides the main page content.
The second argument of @yield provides
a default value when the child view does not define
that section.
The @section directive defines content
in a child view. In a layout, @show
displays a section and marks its end.
Example layout:
<body>
@section('sidebar')
<aside>Default Sidebar</aside>
@show
<main>
@yield('content')
</main>
</body>
Example child view:
@extends('layouts.app')
@section('sidebar')
<aside>Custom Student Sidebar</aside>
@endsection
@section('content')
<h2>Student Dashboard</h2>
@endsection
The child view replaces the default sidebar section with its own content.
The @include directive inserts another
Blade view into the current view.
Create these files:
resources/views/partials/header.blade.php
resources/views/partials/footer.blade.php
Inside header.blade.php:
<header>
<h1>Soopro Pathshala</h1>
<nav>
<a href="/">Home</a>
<a href="/about">About</a>
</nav>
</header>
Inside footer.blade.php:
<footer>
<p>All rights reserved.</p>
</footer>
Include them in a view:
@include('partials.header')
<h2>Welcome to our website</h2>
@include('partials.footer')
Use @include for reusable view fragments
such as headers, footers, alerts, and navigation menus.
Blade components are reusable UI elements that can accept data through attributes.
Create a component:
php artisan make:component Alert
Laravel creates a component class and a view. The view is normally located at:
resources/views/components/alert.blade.php
Inside alert.blade.php:
<div class="alert alert-{{ $type }}" role="alert">
{{ $message }}
</div>
The generated component class should define the
public properties used by the component view.
For example, update
app/View/Components/Alert.php:
<?php
namespace App\View\Components;
use Illuminate\View\Component;
use Illuminate\Contracts\View\View;
class Alert extends Component
{
public string $type;
public string $message;
public function __construct(
string $type = 'info',
string $message = ''
) {
$this->type = $type;
$this->message = $message;
}
public function render(): View
{
return view('components.alert');
}
}
The public properties are available to the component view.
You can render a component using Blade's self-closing component syntax.
<x-alert
type="success"
message="Student saved successfully!"
/>
This displays a Bootstrap success alert, provided Bootstrap's CSS is loaded in your layout.
Another example:
<x-alert
type="danger"
message="Unable to save student."
/>
Component attributes provide a convenient way to customize reusable UI elements without copying the same HTML into multiple pages.
Use suitable validation and trusted values when accepting component properties from dynamic sources.
A slot lets you pass custom content between a component's opening and closing tags.
Create:
resources/views/components/card.blade.php
Inside card.blade.php:
<div class="card shadow-sm">
<div class="card-body">
{{ $slot }}
</div>
</div>
Use the component:
<x-card>
<h3>Student Information</h3>
<p>Name: Rahul</p>
<p>Course: Laravel</p>
</x-card>
Laravel inserts the content supplied between
<x-card> and
</x-card> into
{{ $slot }}.
Components can also use named slots when they need separate content areas such as a title and a body.
Practise creating reusable layouts and components.
layouts/app.blade.php.Remember: use layouts for shared page structures, includes for reusable view fragments, and components for reusable UI elements that accept data or slots.
1. Which directive allows a view to inherit a layout?
2. Which directive inserts another Blade view?
3. What does {{ $slot }} represent in a Blade component?