When we create a laravel based project, then of course we need a master layout file. We make different files for the header, footer, and sidebar also. In this example, I will discuss how to create master layout in laravel 9. This laravel layouts is a must needed things for laravel developers. So let's see how we can create laravel layout header footer.

Laravel provides the @yield() blade directive and @section blade directive to create a master layout file in Laravel. First, we have to create a master layout file and then every time we have to extend it using the @extends() blade directive. Let's see the example:

resources/views/layouts/app.blade.php

<!doctype html>
<html lang="{{ str_replace('_', '-', app()->getLocale()) }}">
<head>
    <meta charset="utf-8">
    <meta name="viewport" content="width=device-width, initial-scale=1">
    <meta name="csrf-token" content="{{ csrf_token() }}">
    <title>{{ config('app.name', 'Laravel') }}</title>
</head>
<body>
    <div id="app">
        <nav class="navbar navbar-expand-md navbar-light bg-white shadow-sm">
            <div class="container">
                <a class="navbar-brand" href="{{ url('/') }}">
                    Laravelia
                </a>
                <div class="collapse navbar-collapse" id="navbarSupportedContent">
                    <!--Header section -->
                </div>
            </div>
        </nav>
        
        <!--Content section -->
        <main class="py-4">
            @yield('content')
        </main>
    </div>
</body>
</html>

 

And now if we want to create a file that will be extended by the master file, then see the below example file:

resources/views/welcome.blade.php

@extends('layouts.app')

@section('content')
<div class="container">
    <div class="row justify-content-center">
        <div class="col-md-8">
            <div class="card">
                <div class="card-header">{{ __('Dashboard') }}</div>

                <div class="card-body">
                    <!-- Page content -->
                </div>
            </div>
        </div>
    </div>
</div>
@endsection

 

Read also: How To Create Custom Blade Directives In Laravel?

 

Conclusion

I have tried to discuss the clear concept of laravel layouts. Now we know how to create master layout in laravel. Hope this laravel blade template layout example tutorial will help you.