$set Not working when using with a relationship
I have an Organisation Resource, an Organisation has one Location which is a mophOne relationship. With the code below I make it possible for the user to input just the postal_code and the number, I have an API which fetches the address based on those two fields. When the result get's back I fill the other fields with the
$set()
function as you can see. All of this works fine, the fields do get populated with the fetched data. But as soon as I press the save button on my resource, only the postal_code
and the number
get updated the other values reset to their original value before the $set happened ending up with an incorrect record in the database with the updated postal_code
and number
and the outdated street
, city
, province
etc.
Why isn't this working? How do I make this work?
Solution:Jump to solution
I have found the bug. The fields that get set with the $set function are disabled, somehow this prevents them from being updated... This is strange behavior, I want the api to populate those fields and don't want them to be changed by the user.
7 Replies
Does it work if you manually fill the form? Are the field
$fillable
?Yes the fields are fillable. Do you mean using $form->fill(); ? I haven’t tried that yet, I’ll let you know if that works out.
No. I mean by typing on your keyboard 😅
Solution
I have found the bug. The fields that get set with the $set function are disabled, somehow this prevents them from being updated... This is strange behavior, I want the api to populate those fields and don't want them to be changed by the user.
I just checked the docs and it seems like I should use
->readOnly()
instead of ->disabled()
...disabled
fields aren't sent to the backend as per HTML spec.I understand but the look of a disabled field is sometimes wanted eventhough it's a readonly field.