Upgrade & Secure Your Future with DevOps, SRE, DevSecOps, MLOps!

We spend hours scrolling social media and waste money on things we forget, but won’t spend 30 minutes a day earning certifications that can change our lives.
Master in DevOps, SRE, DevSecOps & MLOps by DevOps School!

Learn from Guru Rajesh Kumar and double your salary in just one year.


Get Started Now!

What is use of @yield @extend @section in Laravel

In Laravel, the concepts of layouts, yield, extend, and sections are used to create reusable templates and organize the structure of your application’s views. These features make it easier to manage the common elements shared across multiple pages while allowing flexibility to customize specific sections of each page.

Creating a Layout:

First, you’ll create a layout file that defines the common structure and components of your application. This file typically resides in the views/layouts directory. Let’s call it app.blade.php.

Here’s an example of a basic layout file:

<!DOCTYPE html>
<html>
<head>
    <title>@yield('title')</title>
</head>
<body>
    <header>
        <!-- Common header content goes here -->
    </header>

    <main>
        @yield('content')
    </main>

    <footer>
        <!-- Common footer content goes here -->
    </footer>
</body>
</html>

In this layout, you can see that we have defined sections for the title, content, header, and footer. The @yield directives indicate the areas where content from other views can be injected.

Extending the Layout:

To utilize the layout, you need to create views that extend it. These views will inherit the structure and components defined in the layout while allowing you to customize specific sections. Let’s create a view called home.blade.php as an example:

@extends('layouts.app')

@section('title', 'Home')

@section('content')
    <h1>Welcome to our website!</h1>
    <p>This is the content of the home page.</p>
@endsection

In this view, we use the @extends directive to specify that we want to extend the app.blade.php layout. We also use the @section directive to define the content for the title and content sections.

Rendering the View:

To render the final view, you can use the view helper function in your routes or controllers. Here’s an example of how you can render the home.blade.php view:

Route::get('/', function () {
    return view('home');
});

When the home.blade.php view is rendered, Laravel will automatically pull in the content defined in the @section directives and inject them into the corresponding @yield directives in the layout. This allows the final output to include the common layout structure along with the customized content specific to the home page. By using layouts, yield, extend, and sections, you can create a consistent structure for your views while providing the flexibility to customize individual sections. This approach simplifies the management of shared elements and improves the maintainability of your Laravel application’s views.

Related Posts

Understanding Network Protocols and Sockets in Networking

In the world of networking, communication is the key to connecting systems, devices, and applications. Whether you’re browsing the web, sending an email, or transferring files, you’re…

Fixing the “Could not find PHP executable” Error in Live Server on VS Code

this is a common issue and easy to fix! This guide will walk you through the step-by-step solution to get your PHP files running in the browser….

How to Fix the “npm.ps1 cannot be loaded” Error on Windows When Running npm start

If you’re a developer working with React or any Node.js-based projects, you may have encountered the following error when trying to run npm start in PowerShell on…

Simplify Database Migrations with kitloong/laravel-migrations-generator in Laravel

Laravel provides a powerful migration system that allows developers to easily define and manage database schema changes. However, when working with legacy databases or large projects, manually…

Understanding and Fixing the “Unable to Read Key from File” Error in Laravel Passport

Laravel Passport is a powerful package for handling OAuth2 authentication in Laravel applications. It allows you to authenticate API requests with secure access tokens. However, like any…

How to Generate a GitHub OAuth Token with Read/Write Permissions for Private Repositories

When working with GitHub, you may need to interact with private repositories. For that, GitHub uses OAuth tokens to authenticate and authorize your access to these repositories….

0 0 votes
Article Rating
Subscribe
Notify of
guest
0 Comments
Oldest
Newest Most Voted
Inline Feedbacks
View all comments
0
Would love your thoughts, please comment.x
()
x