Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Laravel spatie/laravel-permission @can directive

I'm currently working with Spatie roles and permissions library. I have multiple guards which are admin and web. My admin have permission to 'manage authentication' with guard_name = admin. When I try to run the syntax below it return true.

current_user()->hasPermissionTo('manage authentication', 'admin')

But when I use @can directives the span tag not appear.

@can('manage authentication')
<span>Hello</span>
@endcan

I tried this, but still the span tag is not appearing.

@can('manage authentication', 'admin')
<span>Hello</span>
@endcan

Am I doing it wrongly?

Update: I dont have any roles / permission related property and function inside my user model

like image 516
Sherwin Variancia Avatar asked Nov 29 '25 08:11

Sherwin Variancia


1 Answers

@can not will work with multi guards

You can use this :

@if(auth('guard-name')->check() && auth('guard-name')->user()->can('permission-name'))

after that run :

php artisan cache:forget spatie.permission.cache

then :

php artisan cache:clear
like image 75
Ahmed Rakha Avatar answered Dec 02 '25 03:12

Ahmed Rakha



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!