F
Filament16mo 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 [];
}
}
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.
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: ```php public function panel(Panel $panel): Panel {...
Jump to solution
8 Replies
d3v1anX
d3v1anXOP16mo ago
just for clarify: I didn't use visible or hidden on NavigationItem, because the group will still be shown even if there is no menu item in it.
Patrick Boivin
Patrick Boivin16mo ago
Can you click the SHARE button at the top of the error page and paste the link?
d3v1anX
d3v1anXOP16mo ago
Flare
Target class [hash] does not exist. - The error occurred at https://timetracker.test/company/1
Patrick Boivin
Patrick Boivin16mo ago
Thanks! I'm not entirely sure but I think this could be too early in the Laravel "lifecycle" to access the user instance. Something to try:
return $panel
// ...
->bootUsing(function ($panel) {
$panel->navigationItems($this->getNavigationItems());
})
return $panel
// ...
->bootUsing(function ($panel) {
$panel->navigationItems($this->getNavigationItems());
})
d3v1anX
d3v1anXOP16mo ago
unfortunately now auth()->user is null 😄 https://flareapp.io/share/VP6MO6Rm
Flare
Call to a member function canAccessPanel() on null - The error occurred at https://timetracker.test/company
Solution
d3v1anX
d3v1anX16mo ago
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'));
}
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 🙂
d3v1anX
d3v1anXOP16mo ago
thank you very much @Patrick Boivin 👌
Patrick Boivin
Patrick Boivin16mo ago
Nice solution!
Want results from more Discord servers?
Add your server