Using session in MenuItem

Hi is there any possibility to use session() or auth()->getSession() in MenuItem inside PanelProvider? Following code:
->userMenuItems([
'session' => MenuItem::make()
->label(auth()->getSession()->get('value')),
])
->userMenuItems([
'session' => MenuItem::make()
->label(auth()->getSession()->get('value')),
])
Produces error: Target class [hash] does not exist. If I use session() directly:
->userMenuItems([
'session' => MenuItem::make()
->label(session('value')),
])
->userMenuItems([
'session' => MenuItem::make()
->label(session('value')),
])
Then following error appears: Target class [cache] does not exist.
1 Reply
Dennis Koch
Dennis Koch2w ago
Those are service providers and the session doesn't exist yet. You need to wrap your code in a closure so it evaluated later.
->userMenuItems(fn () => [
'session' => MenuItem::make()->label(session('value')),
])
->userMenuItems(fn () => [
'session' => MenuItem::make()->label(session('value')),
])