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:
but
auth()->id()
is always null, despite being logged in or notSolution:Jump to solution
create a middleware:
php artisan make:middleware YourMiddleware
YourMiddleware.php...25 Replies
you have tu use fn() in AdminPanelPRovider to get your condition evaluate
but i'm not sur if it's working
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
here how i use it:
Oh and here the use for the Auth class:
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:
i get:
not sure if im missing something
so using Auth instead of auth() makes no difference
ok i try myself and you right closure dont work, but i have a workaround for you it's working here:
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
and im logged in
yeah ill set up policies and etc, but i first need this to work ^^
Use a closure see what i do in my if
Whithout closure its not evaluated
okay let me try
hmm like this @sandofgods ??
its just the closure
it doesnt evaluate
yes
r u sure this works?
yes
so why i get a closure instead of the auth user id? š¤·āāļø
im still missing something?
try my full code
you'll see
ur full code always returns false
change the first one to true....
...
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
sorry , vscode confused me š
that is why i was saying it wont work
oh no wait
š
this returns true always:
i guess cuz a closure is truthy
so no, problem not solved xd
Solution
create a middleware:
php artisan make:middleware YourMiddleware
YourMiddleware.php
it works, just tested it out
awesome
didnt know i could do that
now you know āļø
(: yeah ty
thanks y'all š
appreciate it