Laravel-permission: hasrole not working

Created on 28 Aug 2019  路  4Comments  路  Source: spatie/laravel-permission

This code not working

@hasrole('writer') I am a writer! @else I am not a writer... @endhasrole

while this code is working

@if($user->hasRole('writer'))

I am following this link https://docs.spatie.be/laravel-permission/v2/basic-usage/blade-directives/

support

Most helpful comment

All 4 comments

Without seeing your app it's hard to know why it's not working for you.

The tests in this package are passing, and here is how they are written:
https://github.com/spatie/laravel-permission/blob/057affa0b96d48dbdfdab2598d8f52c8ec85c2e6/tests/BladeTest.php#L111-L125
https://github.com/spatie/laravel-permission/blob/master/tests/resources/views/hasRole.blade.php

This stopped working for me too (it worked before).
For me it seems like @role('some-role') @endrole is not recoginzed as blade directive anymore - it is rendered on the HTML page.

The workaround is as already mentioned to use @if(auth()->user()->hasRole('some-role))

This is potentially due to the use of facade/ignition. It takes over the blade compiler causing the following to never fire thus no directives are loaded.
$this->app->afterResolving('blade.compiler', function (BladeCompiler $bladeCompiler) {

Was this page helpful?
0 / 5 - 0 ratings

Related issues

NattananWs picture NattananWs  路  3Comments

tripex picture tripex  路  3Comments

enghelewa picture enghelewa  路  4Comments

notflip picture notflip  路  3Comments

bhulsman picture bhulsman  路  3Comments