dineshsuthar92's avatar

Entrust @role and @permission directives not rendering in blade, laravel 5.6

Let me tell you the points.

-I am using Entrust by https://github.com/Zizaco/entrust/

-I am using multi-auth i.e I have 2 guards as web for users table and admin guard for admins table.

-In admins table there are users with multiple permissions (roles too).

-The permission middelware has the code:

if (!Auth::guard('admin')->user()->can(explode('|', $permissions)))

In my Admin model, I have used use EntrustUserTrait;

Middelware permission is working fine on routes.php the only problem I am facing is if any Admin User does not have permission suppose can-create then the html written in the that block should be hidden in blade file.

@permission('can-create') Lorem Ipsum... @endpermission

the above blade directives are not highlighted as @if or @foreach etc.

I think there might be something due multi-auth I am using, since Entrust takes users table by default.

Kindly enlighten me, considering this situation.

0 likes
2 replies
bobbybouwmann's avatar

Entrust only works with one guard which is defaulted to the web guard. You can change guard, but you can't use both of them. It's just not supported by the package.

You either need to look for another page or implement your own functionality for this ;)

dineshsuthar92's avatar

Yes I got the solution to this issue. Please go through the steps I have used.

-Run command php artisan make:provider EntrustCustomServiceProvider -go to app/Providers/EntrustCustomServiceProvider.php Use Auth; and Write in the boot function

public function boot() {

\Blade::directive('permission', function($expression) {
    return "<?php if (Auth::guard('admin')->user()->can({$expression})) : ?>";
});

\Blade::directive('endpermission', function($expression) {
    return "<?php endif; ?>";
});
}

-Add this entry in providers array in config/app.php : App\Providers\EntrustCustomServiceProvider::class,

One more thing, many of the phpstorm users may wonder why these directives are not auto-populated in the editor then please note, whenever we add custom blade directives, we need to add these in the blade plugin of the phpstorm editor. Here is the solution:

-Go to File->settings->php->Blade -uncheck Default Settings, go to Directives tab. -Add new directive using + -Enter the directive name,here permission without using @, check has parameter -Enter in suffix -same do for @endpermission without adding any parameters.

I hope this is helpful for someone like me :)

Please or to participate in this conversation.