Lesson 13 of 50 – Blade Layouts and Components
26%

Laravel Blade Layouts and Components

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.

1. What Is a Blade Layout?

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.

2. Extend a Layout Using @extends

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.

3. Create Multiple Sections in a Layout

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.

4. Use @section and @show

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.

5. Share Common Sections with @include

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.

6. Create a Blade Component

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.

7. Use Components with Attributes

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.

8. Components with Slots

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.

9. Practical Assignment

Practise creating reusable layouts and components.

  1. Create a common layout named layouts/app.blade.php.
  2. Add a title section and a content section to the layout.
  3. Create home and about views that extend the layout.
  4. Create reusable header and footer partials.
  5. Include the header and footer in a Blade view.
  6. Create an Alert component using Artisan.
  7. Display success and error alerts with different attributes.
  8. Create a Card component that uses the default slot.
  9. Use the components on a student dashboard page.

Remember: use layouts for shared page structures, includes for reusable view fragments, and components for reusable UI elements that accept data or slots.


🧠 Quick Quiz

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?