dynamic navigation item in panel
Hey there,
i tried to create a dynamic navigation items:
The error came up and I don't know where to add the Hash-Class? Can someone help me out?
Thank you
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 [];
}
}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.Target class [hash] does not exist.Thank you
Solution
found a workaround to hide the group as well, if the user cannot access the panel:
This is working right now and I think I can live with that
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'));
}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