Update hidden fields (Relationship)
So I have a select input that is a relation. This field is hidden or shown based on the type of the user. But when type is changed to Customer the relation, the select, should be updated. I know that the field is not being updated because it is hidden. Is their away around this?
9 Replies
Hm, I would expect this to work. So the data should save when the select is visible? π€
It should always save, regardless of the visibility of the select. Since if the type changes from e.g. admin, teamlead to customer. The record in the pivot should be deleted. Because a customer type cannot be associated with groups.
Can you try
->dehydrated(true)
after the hidden method. Maybe that's already enough.Sadly, that doesn't work. After saving the data is still present in the database. Because I changed the type from Teamlead to Customer i expect this table to be empty.
Funny timing, I just had a very similar issue I solved yesterday. Basically hidden or non-visible fields are not updated (even if you manually update the values with for example using
$set
and/or using ->dehydrated()
).
I solved my issue by doing this;
I'm happy to submit a PR later to the docs but I'm not even sure if this is expected behaviour or not? Is it supposed to work when using ->dehydrated(true)
@Dennis Koch ? Actually, aren't all fields ->dehydrated(true)
by default? There is also a ->dehydratedWhenHidden()
method which I played with but was unable to get working, same behaviourHm, so I guess that
$set()
is not working when fields are hidden and not the saving? I am not really sure about this one$set()
itself "works", as in I could change the value to null, then verify and get back the same null using $get()
but it simply doesn't save to the database, it would always skip any hidden fields if you get what I mean?
so it was pretty confusing for me to figure out what was going on, but I am also only using filament for a couple weeks now so still quite newHm, sounds weird if you are using
->dehydratedWhenHidden()
. But I have never used it, so I can't say much about itI had something similar when I wanted to manipulate the value of hidden field ended removing the hidden field and adding logic to https://filamentphp.com/docs/3.x/panels/resources/editing-records#customizing-data-before-saving