F
Filament•14mo ago
ericmp

How to show panel navigation only to certain users?

i want to show only the navigation to the user with id 1 im trying this:
class AdminPanelProvider extends PanelProvider
{
public function panel(Panel $panel): Panel
{
return $panel
->navigation(auth()->id() === 1)

// also tried:
// ->navigation(fn (): bool => auth()->id() === 1)
// ->navigation((fn (): bool => auth()->id() === 1)())
class AdminPanelProvider extends PanelProvider
{
public function panel(Panel $panel): Panel
{
return $panel
->navigation(auth()->id() === 1)

// also tried:
// ->navigation(fn (): bool => auth()->id() === 1)
// ->navigation((fn (): bool => auth()->id() === 1)())
but auth()->id() is always null, despite being logged in or not
Solution:
create a middleware: php artisan make:middleware YourMiddleware YourMiddleware.php...
Jump to solution
25 Replies
sandofgods
sandofgods•14mo ago
you have tu use fn() in AdminPanelPRovider to get your condition evaluate but i'm not sur if it's working
ericmp
ericmpOP•14mo ago
yeah as i said in the 1st message, i tried it using closure too 🤷 i think auth is not there yet, so is not possible to get it but may be a way or i might be missing something, idk
sandofgods
sandofgods•14mo ago
here how i use it:
NavigationItem::make('commission')
->label(fn(): string => __("commission"))
->url(fn (): string => Commissions::getUrl())
->icon('uni-bill-o')
->group(fn(): string => __("settings categorie"))
->hidden(fn(): bool => !Setting::whereTeamsId(Auth::user()->teams_id)->first()->settings_commissions_actif)
->sort(3),
NavigationItem::make('commission')
->label(fn(): string => __("commission"))
->url(fn (): string => Commissions::getUrl())
->icon('uni-bill-o')
->group(fn(): string => __("settings categorie"))
->hidden(fn(): bool => !Setting::whereTeamsId(Auth::user()->teams_id)->first()->settings_commissions_actif)
->sort(3),
Oh and here the use for the Auth class:
use Illuminate\Support\Facades\Auth;
use Illuminate\Support\Facades\Auth;
ericmp
ericmpOP•14mo ago
oh so u use Auth instead of auth() that might be the reason!? i cant try it now, ill @ u when ill try it @sandofgods it doesnt work i did:
->navigation(fn (): bool => \Illuminate\Support\Facades\Auth::user()->id === 1)
->navigation(fn (): bool => \Illuminate\Support\Facades\Auth::user()->id === 1)
i get:
Call to a member function getNavigation() on bool
Call to a member function getNavigation() on bool
not sure if im missing something so using Auth instead of auth() makes no difference
sandofgods
sandofgods•14mo ago
ok i try myself and you right closure dont work, but i have a workaround for you it's working here:
$navigation = false;

if(fn():bool=>Auth::id() ==1){
$navigation = false;
}else {
$navigation = false;
}

return $panel
->navigation($navigation)
$navigation = false;

if(fn():bool=>Auth::id() ==1){
$navigation = false;
}else {
$navigation = false;
}

return $panel
->navigation($navigation)
just do your logic before the return $panel and use your variable to activate or desactivate your menu it's not the must elegant way but its a workaround and it's working But more than doing a search on user id i will create in user model a boolean authorizing the panel to show
ericmp
ericmpOP•14mo ago
No description
ericmp
ericmpOP•14mo ago
and im logged in yeah ill set up policies and etc, but i first need this to work ^^
sandofgods
sandofgods•14mo ago
Use a closure see what i do in my if Whithout closure its not evaluated
ericmp
ericmpOP•14mo ago
okay let me try
ericmp
ericmpOP•14mo ago
hmm like this @sandofgods ??
No description
ericmp
ericmpOP•14mo ago
its just the closure it doesnt evaluate
sandofgods
sandofgods•14mo ago
yes
ericmp
ericmpOP•14mo ago
r u sure this works?
sandofgods
sandofgods•14mo ago
yes
ericmp
ericmpOP•14mo ago
so why i get a closure instead of the auth user id? šŸ¤·ā€ā™‚ļø im still missing something?
sandofgods
sandofgods•14mo ago
try my full code you'll see
ericmp
ericmpOP•14mo ago
ur full code always returns false
sandofgods
sandofgods•14mo ago
change the first one to true....
ericmp
ericmpOP•14mo ago
... it wont work, im not gonna try it thanks for trying to help, but idk how to follow ur advices it works lol my ide not sure wtf vscode doesnt format and in the console it marks it as error but anyways i ran it and it works
ericmp
ericmpOP•14mo ago
No description
ericmp
ericmpOP•14mo ago
sorry , vscode confused me šŸ™Œ that is why i was saying it wont work oh no wait šŸ˜‚ this returns true always:
fn():bool=>Auth::id() ==1
fn():bool=>Auth::id() ==1
i guess cuz a closure is truthy so no, problem not solved xd
Solution
LeandroFerreira
LeandroFerreira•14mo ago
create a middleware: php artisan make:middleware YourMiddleware YourMiddleware.php
public function handle(Request $request, Closure $next): Response
{
$panel = filament()->getCurrentPanel();

if (auth()->id() !== 1) {
$panel->navigation(false);
}

return $next($request);
}
public function handle(Request $request, Closure $next): Response
{
$panel = filament()->getCurrentPanel();

if (auth()->id() !== 1) {
$panel->navigation(false);
}

return $next($request);
}
return $panel
...
->authMiddleware([
Authenticate::class,
YourMiddleware::class,
])
return $panel
...
->authMiddleware([
Authenticate::class,
YourMiddleware::class,
])
ericmp
ericmpOP•14mo ago
it works, just tested it out awesome didnt know i could do that
LeandroFerreira
LeandroFerreira•14mo ago
now you know āœŒļø
ericmp
ericmpOP•14mo ago
(: yeah ty thanks y'all šŸ™Œ appreciate it

Did you find this page helpful?