Can i use in a form a list checkboxex to show or hide other list of checkboxes
In the user model, I have a list of checkboxes that display roles, and another list of checkboxes that show values from a model called 'instruments'. What I'm trying to do is to have the list of instruments displayed when a certain role is selected, and not displayed otherwise. Some of this works, but when I save it, the values disappear even though they are present in the database. If I refresh the page, they appear as I show you in the following video.
Code example
4 Replies
At first glance I think the checkbox list shouldn’t have the same same as your relationship.
@gonzalo2683 Can you share a bit more details on the relationships? How are things setup on the models?
Sure @pboivin
User model, the roles come from the Spatie permissions package.
As @awcodes suggested, I've changed the field names to prevent conflicts with the relationships and it seems to work better now. The last issue I noticed is that when I uncheck the "musico" role and set the instruments to null, it still retains the instruments upon saving. For some reason, the field value is not cleared when using
->when()
.
I've tried commenting out the ->when()
part and indeed the values are correctly unchecked. It seems that something happens when using it in conjunction with ->when()
.
On my phone but you might be creating a race condition. Instead of a get in the all_instruments. I think you should just be setting it from the roles. The relationships should cascade down, at least in my head.