Hide a Toggle field depending on Select option

For some reason, I can't make it work. Here's what I did: Select::make('roles') ->relationship('roles', 'name') ->live(), Toggle::make('is_admin') ->hidden(fn ($record) => !$record->roles->pluck('name')->contains('Vendor'))
30 Replies
BlackShadow
BlackShadow2y ago
Select::make('roles')
->native(false) // Make it look pretty
->relationship('roles', 'name')
->reactive(),

Toggle::make('is_admin')
->hidden(fn ($get) => dd($get('roles')),
Select::make('roles')
->native(false) // Make it look pretty
->relationship('roles', 'name')
->reactive(),

Toggle::make('is_admin')
->hidden(fn ($get) => dd($get('roles')),
Im not sure what $get('roles') returns, can you try this and show what it returns 🙂 ? @Renzo
Renzo
RenzoOP2y ago
Hi @CodeWithDennis, thanks for the reply! So I have 3 roles, Admin, Client, and Vendor. I was hoping to hide some fields if the role selected was Vendor. I'm not sure if it's possible to do if you're using a relationship Select field.
BlackShadow
BlackShadow2y ago
Try the code i sent and send me the response of the dump
Homd
Homd2y ago
It depents on the Select::make('roles') right? You should hide it based on the current state of the form using $get and not the $record
BlackShadow
BlackShadow2y ago
Yep, thats what i sent. 🙂
Homd
Homd2y ago
sorry for echoing your answer. I just read the question and haven't read yours
BlackShadow
BlackShadow2y ago
Np, all he has to do now is check the value of the get and compare it to hide it. Assuming it returns a string
Renzo
RenzoOP2y ago
Sorry, what do you mean by response of the dump? Sorry, been coding for 5 months only 😄
BlackShadow
BlackShadow2y ago
Did you copy my code and try it?
Select::make('roles')
->native(false) // Make it look pretty
->relationship('roles', 'name')
->reactive(),

Toggle::make('is_admin')
->hidden(fn ($get) => $get('roles') == 'Vendor'), // Vendor or vendor (what ever the value is)
Select::make('roles')
->native(false) // Make it look pretty
->relationship('roles', 'name')
->reactive(),

Toggle::make('is_admin')
->hidden(fn ($get) => $get('roles') == 'Vendor'), // Vendor or vendor (what ever the value is)
Renzo
RenzoOP2y ago
Yes, but it changes the UI, hold on let me send it here.
BlackShadow
BlackShadow2y ago
Yes, it will dump the value (bottom of the screen probably)
Renzo
RenzoOP2y ago
Here's what I got
BlackShadow
BlackShadow2y ago
Use ``` code here \ ```
Renzo
RenzoOP2y ago
I'll try this one out, does this work with v3 as well?
No description
BlackShadow
BlackShadow2y ago
select some role yea
Renzo
RenzoOP2y ago
By the way, tried this one too but the Toggle field still shows up.
BlackShadow
BlackShadow2y ago
Please listen 😛 Select a role, and tell me what does the dump return
Renzo
RenzoOP2y ago
Oh still the same, doesn't return anything when I select a role.
BlackShadow
BlackShadow2y ago
did you use my code?
Renzo
RenzoOP2y ago
Yes, this one Select::make('roles') ->relationship('roles', 'name') ->reactive(), Toggle::make('user_preferred') ->hidden(fn ($get) => dd($get('roles')))
BlackShadow
BlackShadow2y ago
Select::make('roles')
->relationship('roles', 'name')
->live(),

Toggle::make('is_admin')
->hidden(fn ($get) => $get('roles') == 'Vendor' || !filled($get('roles')),
Select::make('roles')
->relationship('roles', 'name')
->live(),

Toggle::make('is_admin')
->hidden(fn ($get) => $get('roles') == 'Vendor' || !filled($get('roles')),
Renzo
RenzoOP2y ago
Oh it worked! Tho when I try to select another role again it still shows up. I'll try to do the adjustments 🙂
BlackShadow
BlackShadow2y ago
Just try to mess around with the conditions 🙂
Renzo
RenzoOP2y ago
Oh wait, it shows up when I try to pick any role which is weird
BlackShadow
BlackShadow2y ago
its probably the OR condition Replace || or remove it to test You know what, just change it to:
Toggle::make('is_admin')
->visible(fn ($get) => $get('roles') !== 'Vendor' && filled($get('roles'))),
Toggle::make('is_admin')
->visible(fn ($get) => $get('roles') !== 'Vendor' && filled($get('roles'))),
Renzo
RenzoOP2y ago
Yeah, it still does the same behavior :/ weird, anywho, I'll just stick to boolean like on the documentation
BlackShadow
BlackShadow2y ago
👍
Renzo
RenzoOP2y ago
Thanks 🙂

Did you find this page helpful?