admin管理员组文章数量:1392073
I am working on a Laravel 11 project using Spatie's Laravel Permission package for role-based access control.
I have the following Blade template where I want to conditionally display a sidebar link based on whether the authenticated user has the view_permission_categories permission.
`@php
$userPermissions = Auth::user()->getAllPermissions()->pluck('name')- >toArray();
@endphp
@if(in_array('view_permission_categories', $userPermissions))
<li>
<x-nav-link href="/permissioncategories" :active="request()- >is('permissioncategories')">Permission Categories</x-nav-link>
</li>
@endif
@can('view_permission_categories')
<li>
<x-nav-link href="/permissioncategories" :active="request()- >is('permissioncategories')">Permission Categories</x-nav-link>
</li>
@endcan
`
The Issue: The @if(in_array(...)) condition works as expected, and the link appears. The @can(...) condition does NOT work, meaning the
My Questions: Why does @can('view_permission_categories') fail, while in_array(Auth::user()->getAllPermissions()->pluck('name')->toArray()) works? How can I debug why @can is not recognizing the permission? Is there something specific I need to configure for Spatie's Laravel Permission to make @can work correctly? Any help would be greatly appreciated!
What I Have Tried:
- Checked If the User Has the Permission:
dd(Auth::user()->can('view_permission_categories'));
2.Ensured Roles & Permissions Are Loaded: $currentUser = Auth::user()->load('roles.permissions');
Still, @can does not work.
3.Cleared Cache: php artisan cache:clear php artisan config:clear php artisan view:clear php artisan route:clear php artisan permission:cache-reset
4.Checked AuthServiceProvider.php for Gate Definitions:
`use Illuminate\Support\Facades\Gate;
public function boot()
{
Gate::define('view_permission_categories', function ($user) {
return $user->hasPermissionTo('view_permission_categories');
});
}
`
I am working on a Laravel 11 project using Spatie's Laravel Permission package for role-based access control.
I have the following Blade template where I want to conditionally display a sidebar link based on whether the authenticated user has the view_permission_categories permission.
`@php
$userPermissions = Auth::user()->getAllPermissions()->pluck('name')- >toArray();
@endphp
@if(in_array('view_permission_categories', $userPermissions))
<li>
<x-nav-link href="/permissioncategories" :active="request()- >is('permissioncategories')">Permission Categories</x-nav-link>
</li>
@endif
@can('view_permission_categories')
<li>
<x-nav-link href="/permissioncategories" :active="request()- >is('permissioncategories')">Permission Categories</x-nav-link>
</li>
@endcan
`
The Issue: The @if(in_array(...)) condition works as expected, and the link appears. The @can(...) condition does NOT work, meaning the
My Questions: Why does @can('view_permission_categories') fail, while in_array(Auth::user()->getAllPermissions()->pluck('name')->toArray()) works? How can I debug why @can is not recognizing the permission? Is there something specific I need to configure for Spatie's Laravel Permission to make @can work correctly? Any help would be greatly appreciated!
What I Have Tried:
- Checked If the User Has the Permission:
dd(Auth::user()->can('view_permission_categories'));
2.Ensured Roles & Permissions Are Loaded: $currentUser = Auth::user()->load('roles.permissions');
Still, @can does not work.
3.Cleared Cache: php artisan cache:clear php artisan config:clear php artisan view:clear php artisan route:clear php artisan permission:cache-reset
4.Checked AuthServiceProvider.php for Gate Definitions:
`use Illuminate\Support\Facades\Gate;
public function boot()
{
Gate::define('view_permission_categories', function ($user) {
return $user->hasPermissionTo('view_permission_categories');
});
}
`
Share
Improve this question
edited Mar 15 at 6:02
apokryfos
40.8k11 gold badges81 silver badges125 bronze badges
Recognized by PHP Collective
asked Mar 12 at 8:38
SanskarSanskar
111 bronze badge
2
|
1 Answer
Reset to default 0hmmm, there must be some issue with your traits of polices try the below solutions
Check If Policies or Gates Are Set Up Correctly
dd(Auth::user()->can('view_permission_categories'));
Ensure You're Using Laravel's Built-In Permission Handling
If you're using Spatie's Laravel Permissions Package, make sure you've set up your permissions correctly:
use Spatie\Permission\Traits\HasRoles;
class User extends Authenticatable
{
use HasRoles;
}
Make sure permissions are registered in app/Providers/AuthServiceProvider.php
:
use Illuminate\Support\Facades\Gate;
public function boot()
{
$this->registerPolicies();
Gate::before(function ($user, $ability) {
return $user->hasPermissionTo($ability) ? true : null;
});
}
Check If Your Permission Syncing Is Correct
dd(Auth::user()->getAllPermissions());
Explicitly Check the Role (If Using Role-Based Permissions)
@hasanyrole('admin|editor')
<li>
<x-nav-link href="/permissioncategories" :active="request()->is('permissioncategories')">Permission Categories</x-nav-link>
</li>
@endhasanyrole
I hope from above you get your solution
本文标签:
版权声明:本文标题:php - Laravel Blade @can Not Working, But in_array(Auth::user()->getAllPermissions()) Does - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1744764220a2623938.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
dd(Auth::user()->can('view_permission_categories'));
? If it is true then it quite weird. Try using@if
withauth()->user()->can('view_permission_categories')
– Hoang Commented Mar 12 at 9:51