F
Filamentβ€’16mo ago
JohnnyC

Call auth()->user() from NavigationItems

With Filament V2 I could do that :
Filament::serving(function () {
Filament::registerNavigationItems(
auth()->user()?->groups->map(function ($group) {
return NavigationItem::make($group->name)
->group('Group'))
// ...;
})->toArray() ?? []
);
Filament::serving(function () {
Filament::registerNavigationItems(
auth()->user()?->groups->map(function ($group) {
return NavigationItem::make($group->name)
->group('Group'))
// ...;
})->toArray() ?? []
);
And dynamic items based on authenticated user was generated. But with FIlament v3, i haven't access to auth()->user() or $panel->auth()->use() because it's always null. I know that I have to pass with callback function, but $panel->navigationItems() doesn't allow callback. I tried with $panel->navigation() but all my menu is override and it doesn't work. Have you any idea ?
Solution:
Just did a quick test, this seems to work too: ```php return $panel ->navigationItems([ NavigationItem::make('Test')...
Jump to solution
11 Replies
geisi1909
geisi1909β€’16mo ago
Do you have tried pushing your FilamentPanelServiceProvider in the config/app.php file to the end of the Service Provider config?
Patrick Boivin
Patrick Boivinβ€’16mo ago
@.johnnyc Where are you making the call to auth() in v3? Can you share the code you have been trying?
X7Ryan
X7Ryanβ€’16mo ago
FWIW I'm experiencing this too. I am trying to use auth()->user() inside the visible method for a custom nav item and it's null. @Geisi1909 in my case the only service provider listed after the panel service provider was the RouteServiceProvider. Since the panel was after the AuthServiceProvider that's not it. Still, just to make sure I did move the panel service provider to the very end and it still was null.
X7Ryan
X7Ryanβ€’16mo ago
I recreated it in a bare filament v3 app here: https://github.com/x7ryan/filament-v3-auth-bug
GitHub
GitHub - x7ryan/filament-v3-auth-bug
Contribute to x7ryan/filament-v3-auth-bug development by creating an account on GitHub.
Patrick Boivin
Patrick Boivinβ€’16mo ago
Probably not a silver bullet but I think this should work for custom navs:
class AdminPanelProvider extends PanelProvider
{
public function panel(Panel $panel): Panel
{
// ...
}

public function register()
{
parent::register();

Filament::serving(function () {
// access auth()->user()
});
}
}
class AdminPanelProvider extends PanelProvider
{
public function panel(Panel $panel): Panel
{
// ...
}

public function register()
{
parent::register();

Filament::serving(function () {
// access auth()->user()
});
}
}
Solution
Patrick Boivin
Patrick Boivinβ€’16mo ago
Just did a quick test, this seems to work too:
return $panel
->navigationItems([
NavigationItem::make('Test')
->visible(fn () => auth()->user()->can('test'))
// ...
])
return $panel
->navigationItems([
NavigationItem::make('Test')
->visible(fn () => auth()->user()->can('test'))
// ...
])
X7Ryan
X7Ryanβ€’16mo ago
Interesting....sure enough that works but that can't be intended.
X7Ryan
X7Ryanβ€’16mo ago
That seems more reasonable, though not how it's documented here: https://filamentphp.com/docs/3.x/panels/navigation#conditionally-hiding-navigation-items so the question is, is it a bug with Filament that the docs version doesn't work or is it a mistake in the docs?
Patrick Boivin
Patrick Boivinβ€’16mo ago
Yes probably a small glitch in the docs. Feel free to send a PR if you have some time πŸ˜„
X7Ryan
X7Ryanβ€’16mo ago
GitHub
Update Docs for conditionally showing a Nav Item by x7ryan Β· Pull R...
While trying to conditionally show a nav item I would receive an exception that auth()->user() was null despite following the example on the docs. Someone else seemed to have the same issue. Aft...
JohnnyC
JohnnyCOPβ€’16mo ago
Thank you guys ! I just read the discussions, thank you for these instructive exchanges πŸ‘
Want results from more Discord servers?
Add your server