F
Filament9mo ago
Anto03

User in admin panel

Hello, how can i access the auth()-> user() inside a panel. i tried with callback function and it still doesn t work. Could you help me? I want to verify if user hasRole() to make a custom navigation, i use modules .
27 Replies
ConnorHowell
ConnorHowell9mo ago
Could you share some actual code of your current attempt? That would help give us some more context to figure out why it's not working
Thijmen
Thijmen9mo ago
Are you tring to access it in a PanelProvider? In all resources it should work In PanelProvider you can't use auth()->user() because thats to early in the lifecycle
Anto03
Anto039mo ago
yes, i want to access kn PanelProvider
Thijmen
Thijmen9mo ago
You can't access auth()->user() there
Anto03
Anto039mo ago
what alternative method can i use?
Thijmen
Thijmen9mo ago
nvm
Dennis Koch
Dennis Koch9mo ago
->visible()/->hidden() with callbacks
Thijmen
Thijmen9mo ago
You should be able to do it In the right methods
NavigationItem::make('Test')
->visible(fn () => auth()->user()?->hasRole('test'))
NavigationItem::make('Test')
->visible(fn () => auth()->user()?->hasRole('test'))
Anto03
Anto039mo ago
No description
Thijmen
Thijmen9mo ago
Try auth()->user()->can()
Anto03
Anto039mo ago
it is the same for hasRole
Thijmen
Thijmen9mo ago
Also it's Auth::user()->can() Not Auth::can()
Thijmen
Thijmen9mo ago
Laravel - The PHP Framework For Web Artisans
Laravel is a PHP web application framework with expressive, elegant syntax. We’ve already laid the foundation — freeing you to create without sweating the small things.
Anto03
Anto039mo ago
$navigationItems[] = NavigationItem::make($key) ->url("admin/$key_mod") ->visible(fn () => auth()->user()?->hasRole('user_panel')) ->icon('heroicon-o-document') ->group('Modules') ->sort(1);
Thijmen
Thijmen9mo ago
Please read the docs Thats not how you make an array
Anto03
Anto039mo ago
public function Navigation($module, $main) { $modules = json_decode(file_get_contents(base_path('modules_statuses.json')), true); $navigationItems = []; if ($main === 'not') { $navigationItems[] = NavigationItem::make('Main Dashboard') ->url("/admin"); } foreach ($modules as $key => $value) { $path = base_path("Modules/$key/Filament/Admin/Resources/*Resource.php"); $key_mod = strtolower($key); if ($key !== $module) { if($main !== 'not'){ $navigationItems[] = NavigationItem::make($key) ->url("admin/$key_mod") ->visible(fn () => auth()->user()?->hasRole('user_panel')) ->icon('heroicon-o-document') ->group('Modules') ->sort(1); }else{ $navigationItems[] = NavigationItem::make($key) ->url("$key_mod") ->icon('heroicon-o-document') ->group('Modules') ->sort(1); } } } return $navigationItems; }
Thijmen
Thijmen9mo ago
NavigationItem::make() needs to be inside ->navigationItems([]) Look at the docs This is very simple. Just read it
Thijmen
Thijmen9mo ago
Yeah so whats the problem?
Anto03
Anto039mo ago
i can t access the user inside it, i want to give access to navigation by using his role
Thijmen
Thijmen9mo ago
Just make the navigation inside of the panel provider
Anto03
Anto039mo ago
No description
Anto03
Anto039mo ago
i have the same error <?php namespace App\Helpers; use Filament\Navigation\NavigationItem; class NavigationHelper { public function Navigation($module, $main) { $modules = json_decode(file_get_contents(base_path('modules_statuses.json')), true); $navigationItems = []; if ($main === 'not') { $navigationItems[] = NavigationItem::make('Main Dashboard') ->url("/admin"); } foreach ($modules as $key => $value) { $path = base_path("Modules/$key/Filament/Admin/Resources/*Resource.php"); $key_mod = strtolower($key); if ($key !== $module) { if($main !== 'not'){ $navigationItems[] = NavigationItem::make($key) ->url("admin/$key_mod") ->icon('heroicon-o-document') ->group('Modules') ->sort(1); }else{ $navigationItems[] = NavigationItem::make($key) ->url("$key_mod") ->icon('heroicon-o-document') ->group('Modules') ->sort(1); } } } return $navigationItems; } }
ConnorHowell
ConnorHowell9mo ago
Does this actually cause an exception when running the code? Looks more like an IDE issue since the response of user() is Authenticatable and not the User model which is why the IDE thinks hasRole doesn't exist