Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Laravel 5.2 conditional extends template in Blade

I want to extend template based on condition. I know i can use @if @else statement in blade. I am doing the same thing, but blade extending both the template. I don't know why.

@if(isset(Auth::user()->id))
    @extends('layouts.adminlayout')
@else
   @extends('layouts.default')
@endif

@section('content')
    i am the home page
    {{ isset(Auth::user()->id) }}
@stop

As, you can see i am checking whether or not user login-ed and then extend the template layout. But it is extending from both the layout.

Please help me.

like image 837
Mandy Avatar asked Mar 04 '16 09:03

Mandy


2 Answers

The first line in your extended blade view must be the @extends directive. Try using a ternary operator for this.

@extends(isset(Auth::user()->id) ? 'layouts.adminlayout' : 'layouts.default');

UPDATE for role based layouts. Refer to this question for more conditions.

@extends((!isset(Auth::user()->id))? 'layouts.default': ((Auth::user()->role == 'admin') ? 'layouts.adminlayout' : 'layouts.moderatorlayout'));
like image 186
Nabin Kunwar Avatar answered Nov 13 '22 05:11

Nabin Kunwar


Try this code, parenterais are important

@extends(Auth::user()->rol_id == 1 ? 'layouts.admin' : ((Auth::user()->rol_id == 2) ? 'layouts.client' : 'layouts.client'))
like image 43
Luisfayre Avatar answered Nov 13 '22 03:11

Luisfayre