Laravel blade `#auth` directive not working as expected - php

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)

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

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.
Manager : Auth::guard('manager')->user()
Admin : Auth::user()
Customer : Auth::guard('customer')->user()

Related

Content esclusion on some page

Hi all i will be needing an assistance. I have a navbar that all link on my app use but i will like to seclude a search form from some of the link in the app but am having difficulty doing that. This what i have tried on my nabvar so far example
Tried using route but no success
#if( (route('portfolio') &&(route('portfolio.details)
content here
#else
content
#endif
then i tried targeting the URI
#if( Request::is('/portfolio') &&(route('portfolio/details)
content here
#else
content
#endif
None seems to working.
I don't think you should be using the route helper method in your if statement. I think if you just do something like this, it should work.
#if (request()->is('portfolio'))
content
#else
content
#endif

Laravel Blade Compiler

I have problem with:
Blade::compileString()
I have article view:
{!! Blade::compileString($article->content) !!}
My $article->content contain:
<p>Test</p>
<p>{{ module('contact') }}</p>
And I don't know why blade compiling it to:
source screenshot
Why module() function is not executing?
Function calls are never compiled.
Let's say your view shows the current time and you'd call {{ date('H:i:s') }} in order to do that. When compiling, that will change to a certain timestamp, say 12:34:56. Now everytime an user refreshes the page, even hours later, the time won't change anymore until the view is next compiled, which would have the undesired effect of basically freezing your entire page.
Hope this makes sense.

Laravel Blade conditional sections

I want to show in my view one section if conditional is true, other section if it is false. I am using PhpStorm as IDE and what I get in those cases is that directive is not closed:
#if (conditional)
#section('content')
...
#stop
#else
#include('pages.something')
#endif
I am getting that opening #if is not closed, and that #else is missing opening directive.
Front-end works fine, but this is really bugging me in IDE. Can it be resolved?
EDIT:
This only happens when using conditionals and sections together. Conditionals and includes work fine.
Try to wrap your content using #yield
See laravel's docs for reference. https://laravel.com/docs/5.5/blade#template-inheritance

Using Sessions in Blade shows blank page

I have this in my blade file:
#if(Session::has('logged'))
#include('layouts.usernav')
#else
#include('layouts.publicnav')
#endif
When I run my page, it is just a blank page.
When I change it to look like this:
#if(1 == 1)
#include('layouts.usernav')
#else
#include('layouts.publicnav')
#endif
My page gets displayed. I don't know what I am doing wrong. What would/could be causing this?

Laravel Blade: Try if imported php file exists

How would you do this using blade's #import method? I've tried:
#if (#include('/path/to/phpfile'))
#include('/path/to/phpfile')
#else
<h2>Oops! It doesn't look like this page exists!</h2>
#endif
also tried with file_exists(), no dice :(
You may try this:
#if(View::exists('viewname'))
#include('viewname')
#endif

Categories