Laravel blade `@auth` directive not working as expected

I am trying to pick the master blade template dynamically as per the current user roll logged in. (here it should go to the 'shopowner' auth block)

@auth('shopmanager')
@extends('theme::Admins.shopmanager.layout.master')
@endauth

@auth('shopowner')
@extends('theme::Admins.shopowner.layout.master')
@endauth

but this always gives error as it tries to compile the 'shopmanager' master template. It is not going into the 'shopmanager' @auth block because it's not printing anything if I print inside that block.

It only works if I completely comment out that line.

P.S.:

This is the master theme::Admins.shopmanager.layout.master template file which must not be loaded.

@extends('theme::Admins.outline.layout.master')

@include('theme::Admins.shopmanager.layout.common.header')
@include('theme::Admins.shopmanager.layout.common.left-sidebar') // The error throws from inside this view.
@include('theme::Admins.shopmanager.layout.common.footer')

@section('title-head', __('Shop Manager'))

I can wrap the @auth check around @include lines but the point is, this complete file should be skipped from the compilation.

SOLVED

As per my learning, @extend(...) will always be compiled regardless of outer wrap conditions. so must be moved to dynamic variable based blocks.

@auth('shopmanager')
    @php
        $masterTemplate = 'theme::Admins.shopmanager.layout.master';    
    @endphp
@endauth

@auth('shopowner')
    @php
        $masterTemplate = 'theme::Admins.shopowner.layout.master';    
    @endphp
@endauth

@extends($masterTemplate)

Laravel blade `@auth` condition not working as expected, Laravel blade `@auth` condition not working as expected. Posted 1 year ago by amitshahc. I am trying to pick the master blade template� Bug report What I did: Changed the user model in the "Providers" section in config/auth.php to Backpack\Base\app\Models\BackpackUser::class (from default App\User::class) as specified in the setup instructions What I expected to happen:

Try below code ,i hope this ans help you:

   @if(Auth::check())
      @if(Auth::user()->role=='shopmanager')
          @extends('theme::Admins.shopmanager.layout.master')
      @else
          @extends('theme::Admins.shopowner.layout.master')
      @endif
    @endif

Blade directive's not working, In Laravel 5.7 it worked and now when i upgrade to 5.8 it stoped working. It wil print True like expected, so only the custom directives are not working. the role directive works. is there something wrong with my setup why you original doesn't work for me? of is there a change in laravel why this change is needed? i see everyone at entrust having problems with the blade directives are advised to replace their serviceprovider, so maybe there's a change in laravel or something? Zizaco/entrust#418

If you have multi-authentication that time try below :

@if(Auth::guard('shopmanager')->user())
   @extends('')
@else
   @extends('')
@endif

In my case, I'm handling manager, customer and admin with this code.

  1. Manager : Auth::guard('manager')->user()
  2. Admin : Auth::user()
  3. Customer : Auth::guard('customer')->user()

Authorization - Laravel, Via The User Model; Via Middleware; Via Controller Helpers; Via Blade You do not need to choose between exclusively using gates or exclusively any , none , authorize , can , cannot ) and the authorization Blade directives ( @can doesn't contain a method with a name matching the name of the ability being checked. And in the blade.php file I call this like: @routes() But the blade just returnes this like plain text and not like the routes it should give. Nou I have tried this in a new project and there it works. In Laravel 5.7 it worked and now when i upgrade to 5.8 it stoped working. And when I use standaard directives like:

@error directive not working as documented � Issue #31240 � laravel , Laravel Version: 6.12.0 PHP Version: 7.2.4 Database Driver & Version: is not displaying the error message as documented/expected. @error directive not working as documented #31240 Submit the form. I am having the same issue, i have installed laravel and did all the steps required for Auth. Blade includes lots of built-in directives. But, this tutorial will help you with laravel blade directives that you’ll often reach out during your time in laravel. 1. Check whether the user is a guest or not using blade directive This helps to check if the user is a guest i.e., not an authenticated user. For this purpose we use:

blade directives not working � Issue #278 � spatie/laravel-permission , check for that token on the POST side) to every form on the site. Dependency injection Instead of instantiating dependencies in a class, expect them to be injected in Eager loading Avoiding N+1 problems by adding a second smart query to Echo A Laravel product that makes WebSocket authentication and syncing of� As part of Laravel 5.5 version release, Laravel introduced new blade directives called @auth and @guest.As the name implies both can be useful to determine if the current user is authenticated or guest. In the previous versions of Laravel to check user authentication status we'd used @if directive with the combination of Auth()->check(). But the

Laravel: Up and Running: A Framework for Building Modern PHP Apps, Directive Blade syntax options like Eagerloading Avoiding N+1 problems by adding a sec‐ond smart query to Echo A Laravel product that makes WebSocket authentication and syncing of data simple. Environment variables Variables that are defined in an .env file that is expected to be excluded from ver‐ sion control. If you are starting a brand new application and would like to include the authentication scaffolding, you may use the --auth directive when creating your application. This command will create a new application with all of the authentication scaffolding compiled and installed: laravel new blog --auth. Views

Comments
  • I think you have typo on last line. It needs to be @endauth. Maybe you have an issue in your auth guards? Are you sure that "shopowner" correspond to one of the guards configured in your auth.php?
  • You have two role (1)shopmanager (2)shopowner. right??
  • @wau, typo i updated. but that was not the issue.
  • @Manisha, role is not some column name in table. i am using guard of laravel.
  • This should be marked as best answer, I was trying to understand why extends was ignoring my conditions (displaying both mobile and desktop version). You totally saved my day!
  • I already had tried if (Auth::guard('shopmanager')->check()) syntax before and still the same issue. tried your syntax also.
  • The error comes from one of the view which is included in 'shopmanager's' master template which we extending in above code. in that view the $user variable obviously can't be found and that's why we don't want to load the complete master view wrapper.
  • I think the problem is not here,but in master app file.
  • I have added the master template file too. but my point is.. why even it is going inside that to check the include files?
  • First you are check shopmanager role is available or not? try this and check anythink getting or not @if(Auth::user()->role=='shopmanager') dd(Auth::user()->role) @endif
  • This didn't work for me. I bet the @extend still goes for compilation regardless of the condition. check my solution which worked will explain it.
  • have you ever tried to @include(..) another template inside your master template(which is not logged in) and there print some undefined variable. you will catch the point.