How to add layout in blade in laravel?

by elise_daugherty , in category: PHP Frameworks , 6 months ago

How to add layout in blade in laravel?

Facebook Twitter LinkedIn Telegram Whatsapp

1 answer

Member

by rollin , 5 months ago

@elise_daugherty 

To add a layout in a Blade file in Laravel, you can use the @extends directive.

  1. Create a layout file in the resources/views/layouts directory. For example, you can create a file named app.blade.php with the common HTML structure that you want to include in all your pages.
  2. Use the @yield directive in the layout file to define sections that will be replaced by content from the child view files. For example, you can include a @yield('content') in the layout file where you want the content from the child view to be displayed.
  3. In your child view files, use the @extends directive to specify the layout file to be used and the @section directive to define the content for the @yield sections. For example:
1
2
3
4
5
@extends('layouts.app')

@section('content')
    // Your content here
@endsection


  1. You can now use the child view file in your routes or controllers to render the complete HTML page with the layout. Use the view() function to render the child view file. For example, in a route definition:
1
2
3
Route::get('/', function () {
    return view('childview');
});


By following these steps, you can easily add a layout to your Blade files in Laravel and maintain a consistent structure across your application.