F
Filament3d ago
Yor

Navigation for multi role panel

Hello, I have 2 role super admin and admin on the panel basically it shares a same panel . I need to hide some navigation if he is appear as admin but if he is super admin he can access all navigation and navigate to there, I only want to hide the Admin group section nav and the Other group section. Is there a way to achieve that?
No description
9 Replies
charlie
charlie3d ago
I think for resources a policy should be enough. And for non resources, you could do the following:
->navigationItems([
NavigationItem::make('Horizon')
->url('/horizon', shouldOpenInNewTab: true)
->group('Admin')
->visible(fn(): bool => auth()->user()->hasRole('super_admin')),
->navigationItems([
NavigationItem::make('Horizon')
->url('/horizon', shouldOpenInNewTab: true)
->group('Admin')
->visible(fn(): bool => auth()->user()->hasRole('super_admin')),
Yor
YorOP3d ago
I already tried like that that way but its resulting the navigation items still can be show by admin though
charlie
charlie3d ago
If they're regular resources, make sure your policy is correct. maybe the role query isn't ok?
Yor
YorOP3d ago
you mean this one? <?php namespace App\Policies; use App\Models\Brand; use App\Models\User; class BrandPolicy { /** * Create a new policy instance. */ public function __construct() { // } public function manageOwn(User $user, Brand $brand) { return $user->hasRole('super-admin') || $user->id === $brand->user_id; // Superadmin or owner can manage } }
charlie
charlie3d ago
in your case it's the viewAny() method
Mohamed Ayaou
Mohamed Ayaou3d ago
try to override the shouldRegisterNavigation resource method:
public static function shouldRegisterNavigation(): bool
{
return auth()->user()->hasRole('super_admin');
}
public static function shouldRegisterNavigation(): bool
{
return auth()->user()->hasRole('super_admin');
}
Wrax
Wrax2d ago
https://laravel.com/docs/11.x/authorization#policy-filters
use App\Models\User;

/**
* Perform pre-authorization checks.
*/
public function before(User $user, string $ability): bool|null
{
if ($user->isSuperAdmin()) {
return true;
}

return null;
}
use App\Models\User;

/**
* Perform pre-authorization checks.
*/
public function before(User $user, string $ability): bool|null
{
if ($user->isSuperAdmin()) {
return true;
}

return null;
}
Laravel - The PHP Framework For Web Artisans
Laravel is a PHP web application framework with expressive, elegant syntax. We’ve already laid the foundation — freeing you to create without sweating the small things.
Yor
YorOP2d ago
thankyou, the navigation can be hidden now

Did you find this page helpful?