F
Filament14mo 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
ConnorHowell14mo 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
Thijmen14mo 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
Anto03OP14mo ago
yes, i want to access kn PanelProvider
Thijmen
Thijmen14mo ago
You can't access auth()->user() there
Anto03
Anto03OP14mo ago
what alternative method can i use?
Thijmen
Thijmen14mo ago
nvm
Dennis Koch
Dennis Koch14mo ago
->visible()/->hidden() with callbacks
Thijmen
Thijmen14mo 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
Anto03OP14mo ago
No description
Thijmen
Thijmen14mo ago
Try auth()->user()->can()
Anto03
Anto03OP14mo ago
it is the same for hasRole
Thijmen
Thijmen14mo ago
Also it's Auth::user()->can() Not Auth::can()
Thijmen
Thijmen14mo 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
Anto03OP14mo 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
Thijmen14mo ago
Please read the docs Thats not how you make an array
Anto03
Anto03OP14mo 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
Thijmen14mo ago
NavigationItem::make() needs to be inside ->navigationItems([]) Look at the docs This is very simple. Just read it
Anto03
Anto03OP14mo ago
Thijmen
Thijmen14mo ago
Yeah so whats the problem?
Anto03
Anto03OP14mo ago
i can t access the user inside it, i want to give access to navigation by using his role
Thijmen
Thijmen14mo ago
Just make the navigation inside of the panel provider
Anto03
Anto03OP14mo ago
No description
Anto03
Anto03OP14mo 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; } }
Anto03
Anto03OP14mo ago
Anto03
Anto03OP14mo ago
ConnorHowell
ConnorHowell14mo 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
Want results from more Discord servers?
Add your server