How to Hide Panel Icons Based on User Roles and Order Panels
I'm working on a Laravel Filament project with multiple panels, and I need to customize the visibility of these panels based on user roles. Specifically, I want to:
Hide panel icons if a user’s role doesn’t have access to that panel. Maintain a specific order for the panel display.(admin, doctorants,.) Here’s my current setup:
I'm using the bezhansalleh/filament-shield package for role management. Each user can have access to multiple panels, and I store panel access information in a panel_accesses table, which associates roles with panel IDs.
class User extends Authenticatable implements FilamentUser{ use HasRoles; // from spatie/laravel-permission public function canAccessPanel(Panel $panel): bool { if ($this->hasRole('super_admin')) { return true; } foreach ($this->roles as $role) { if (PanelAccess::where('role_id', $role->id) ->where('panel_id', $panel->getId()) ->exists()) { return true; } } return false; }}
class User extends Authenticatable implements FilamentUser{ use HasRoles; // from spatie/laravel-permission public function canAccessPanel(Panel $panel): bool { if ($this->hasRole('super_admin')) { return true; } foreach ($this->roles as $role) { if (PanelAccess::where('role_id', $role->id) ->where('panel_id', $panel->getId()) ->exists()) { return true; } } return false; }}