F
Filamentβ€’7mo ago
Patrick

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
Dennis Koch
Dennis Kochβ€’7mo ago
Put it inside a closure. You probably register this in a ServiceProvider and auth only exists after the middleware ->visible(fn () => auth()->...)
Patrick
PatrickOPβ€’7mo ago
@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?
Dennis Koch
Dennis Kochβ€’7mo ago
It returns an array. You could loop through them and apply it to ever item. ->items() might support a closure to
Patrick
PatrickOPβ€’7mo ago
Thanks! Sadly items() does not support a closure, so I will add items manually and do a check there πŸ™‚
Dennis Koch
Dennis Kochβ€’7mo ago
Btw when this is on a PanelProvider the whole ->navigationItems() might accept a closure πŸ˜…
Patrick
PatrickOPβ€’7mo ago
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()
...ShippingDocumentResource::getNavigationItems()
...ShippingDocumentResource::getNavigationItems()
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()
->navigation(function (NavigationBuilder $builder): NavigationBuilder {
return $builder
->items(
Pages\Dashboard::getNavigationItems()
)
->groups([
NavigationGroup::make()
->label(__('Order Management'))
->items(auth()->user()->hasAnyRole(
...
)?[
NavigationItem::make(__('Orders'))
->url(fn():string => OrderResource\Pages\ListOrders::getUrl())
->icon('heroicon-o-rectangle-stack')
->isActiveWhen(fn (): bool => request()->routeIs(OrderResource\Pages\ListOrders::getRouteName())),
...ShippingDocumentResource::getNavigationItems(),
...ReturnableResource::getNavigationItems(),
]:[]),
...
->navigation(function (NavigationBuilder $builder): NavigationBuilder {
return $builder
->items(
Pages\Dashboard::getNavigationItems()
)
->groups([
NavigationGroup::make()
->label(__('Order Management'))
->items(auth()->user()->hasAnyRole(
...
)?[
NavigationItem::make(__('Orders'))
->url(fn():string => OrderResource\Pages\ListOrders::getUrl())
->icon('heroicon-o-rectangle-stack')
->isActiveWhen(fn (): bool => request()->routeIs(OrderResource\Pages\ListOrders::getRouteName())),
...ShippingDocumentResource::getNavigationItems(),
...ReturnableResource::getNavigationItems(),
]:[]),
...
Patrick
PatrickOPβ€’7mo ago
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...
Want results from more Discord servers?
Add your server