phplaravellaravel-6

How can I include an extend a layout file in Laravel?


I am having difficulty extending to different layout file according to the if else condition:

@if($subdomain_alias === "blk")
    @extends("layouts.frontend-new")
    @section('title')
    Home
    @endsection
    @section('content')
    {{-- Some Content  --}}
    @endsection
@else
    @extends('layouts.frontend')
    @section('title')
    Home
    @endsection
    @section('content')
    {{-- Some Content  --}}
    @endsection
@endif

But the problem is both the layout is included and I can see everything twice.

I tried using @include but the view contents are not loaded in that.

enter image description here

The header appears twice. How can I solve that?


Solution

  • you need to use a ternary operator for selecting between different master layout in first line of your blade file:

    @extends(($subdomain_alias === "blk" ? "layouts.frontend-new" : "layouts.frontend"))
    
    @section('title')
    Home
    @endsection
    
    @section('content')
    {{-- Some Content  --}}
    @endsection