FilamentF
Filamentβ€’3y ago
d3v1anX

dynamic navigation item in panel

Hey there,

i tried to create a dynamic navigation items:

public function panel(Panel $panel): Panel
{
    return $panel
    //...
    ->navigationItems($this->getNavigationItems());
}

private function getNavigationItems()
{
    if (\auth()->user()->canAccessPanel(Filament::getPanel('admin'))) {
        return [
            NavigationItem::make(fn () => __('Login as admin'))
                ->url('/admin')
                ->icon('heroicon-o-arrow-right-on-rectangle')
                ->visible(fn () => auth()->user()->canAccessPanel(Filament::getPanel('admin')))
                ->group('Administration')
                ->sort(2),
        ];
    } else {
        return [];
    }
}


The error
Target class [hash] does not exist.
came up and I don't know where to add the Hash-Class? Can someone help me out?
Thank you πŸ™‚
Solution
found a workaround to hide the group as well, if the user cannot access the panel:

public function panel(Panel $panel): Panel
    {
      return $panel
      // ... 
      ->navigationItems([
                NavigationItem::make(fn () => __('Login as admin'))
                    ->url('/admin')
                    ->icon('heroicon-o-arrow-right-on-rectangle')
                    ->visible(fn () => $this->canAccessAdminPanel())
                    ->sort(2)
                    ->group(fn () => $this->canAccessAdminPanel() ? __('Administration') : ''),
            ]);
    }

private function canAccessAdminPanel()
    {
        return auth()->user()->canAccessPanel(Filament::getPanel('admin'));
    }


This is working right now and I think I can live with that πŸ™‚
Was this page helpful?