F
Filamentā€¢4mo 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ā€¢4mo ago
you have tu use fn() in AdminPanelPRovider to get your condition evaluate but i'm not sur if it's working
ericmp
ericmpā€¢4mo 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ā€¢4mo 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
ericmpā€¢4mo 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ā€¢4mo 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
ericmpā€¢4mo ago
No description
ericmp
ericmpā€¢4mo ago
and im logged in yeah ill set up policies and etc, but i first need this to work ^^
sandofgods
sandofgodsā€¢4mo ago
Use a closure see what i do in my if Whithout closure its not evaluated
ericmp
ericmpā€¢4mo ago
okay let me try
ericmp
ericmpā€¢4mo ago
hmm like this @sandofgods ??
No description
ericmp
ericmpā€¢4mo ago
its just the closure it doesnt evaluate
sandofgods
sandofgodsā€¢4mo ago
yes
ericmp
ericmpā€¢4mo ago
r u sure this works?
sandofgods
sandofgodsā€¢4mo ago
yes
ericmp
ericmpā€¢4mo ago
so why i get a closure instead of the auth user id? šŸ¤·ā€ā™‚ļø im still missing something?
sandofgods
sandofgodsā€¢4mo ago
try my full code you'll see
ericmp
ericmpā€¢4mo ago
ur full code always returns false
sandofgods
sandofgodsā€¢4mo ago
change the first one to true....
ericmp
ericmpā€¢4mo 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
ericmpā€¢4mo ago
No description
ericmp
ericmpā€¢4mo 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ā€¢4mo 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
ericmpā€¢4mo ago
it works, just tested it out awesome didnt know i could do that
LeandroFerreira
LeandroFerreiraā€¢4mo ago
now you know āœŒļø
ericmp
ericmpā€¢4mo ago
(: yeah ty thanks y'all šŸ™Œ appreciate it
Want results from more Discord servers?
Add your server
More Posts