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
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
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 lifecycleyes, i want to access kn PanelProvider
You can't access
auth()->user()
therewhat alternative method can i use?
nvm
->visible()
/->hidden()
with callbacksYou should be able to do it
In the right methods
Try
auth()->user()->can()
it is the same for hasRole
Also it's
Auth::user()->can()
Not Auth::can()
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.
$navigationItems[] = NavigationItem::make($key)
->url("admin/$key_mod")
->visible(fn () => auth()->user()?->hasRole('user_panel'))
->icon('heroicon-o-document')
->group('Modules')
->sort(1);
Please read the docs
Thats not how you make an array
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;
}
NavigationItem::make()
needs to be inside ->navigationItems([])
Look at the docs
This is very simple. Just read itYeah so whats the problem?
i can t access the user inside it, i want to give access to navigation by using his role
Just make the navigation inside of the panel provider
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;
}
}
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