How can I hide NaviationItems in custom NavigationGroup based on user role
Hi there,
As I am using getNavigationItems() it seems not possible? Or is there a way to hide each item based on a user role?
NavigationGroup::make()
->label(__('Reporting'))
->items([
...OrderReportingResource::getNavigationItems()->visible(auth()->user()->hasRole('OrderReportingUser')),
...OrderItemReportingResource::getNavigationItems(),
...ProductReportingResource::getNavigationItems(),
]),
7 Replies
Put it inside a closure. You probably register this in a ServiceProvider and auth only exists after the middleware
->visible(fn () => auth()->...)
@Dennis Koch Thanks for your fast reply, I added a closure but the problem is more that visible() is not available on getNavigationItems(). Any ideas if there is way to solve it except adding all pages manually?
It returns an array. You could loop through them and apply it to ever item.
->items()
might support a closure toThanks!
Sadly items() does not support a closure, so I will add items manually and do a check there π
Btw when this is on a PanelProvider the whole
->navigationItems()
might accept a closure π
Mhh but I am using a custom navigation, so ->navigationItems is not what I am using or?
I am using your build in :getNavigationItems() on a resource which neither accepts a closure nor I have access to visible() or hide()
Custom Nav code part. So for example for Order Management I can hide the whole nav group by returning empty array if my auth check fails. But no clue how I can do it for single getNavigationItems()
Okay...sorry it's not working with new login. I found the solution here: https://github.com/filamentphp/filament/issues/7288
GitHub
I cannot access to auth()->user() in panel Β· Issue #7288 Β· filament...
Package filament/filament Package Version v3.0.0-beta21 Laravel Version v10.0.0 Livewire Version v3.0.0 PHP Version 8.2.4 Problem description auth()->user() return null in panel class AdminPanel...