Call auth()->user() from NavigationItems
With Filament V2 I could do that :
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:Jump to solution
Just did a quick test, this seems to work too:
```php
return $panel
->navigationItems([
NavigationItem::make('Test')...
11 Replies
Do you have tried pushing your FilamentPanelServiceProvider in the config/app.php file to the end of the Service Provider config?
@.johnnyc Where are you making the call to
auth()
in v3? Can you share the code you have been trying?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.
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.
Probably not a silver bullet but I think this should work for custom navs:
Solution
Just did a quick test, this seems to work too:
Interesting....sure enough that works but that can't be intended.
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?
Yes probably a small glitch in the docs. Feel free to send a PR if you have some time π
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...
Thank you guys ! I just read the discussions, thank you for these instructive exchanges π