$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?
Section::make('Adresgegevens')
->collapsible()
->columns(2)
->relationship('location')
->afterStateUpdated(function (?array $state, ?array $old, Set $set) {
if ($state['country_code'] !== 'NL') {
return;
}

if ($state['postal_code'] === $old['postal_code']
&& $state['number'] === $old['number']) {
return;
}

$locationData = LocationData::from([
'postal_code' => $state['postal_code'],
'number' => $state['number'],
'addition' => $state['addition'],
])->updateUsingAPI()->toArray();

$set('location.street', $locationData['street'] ?? null);
$set('location.city', $locationData['city'] ?? null);
$set('location.municipality', $locationData['municipality'] ?? null);
$set('location.province', $locationData['province'] ?? null);
$set('location.lat', $locationData['lat'].'' ?? null);
$set('location.long', $locationData['long'].'' ?? null);
})
->schema([
...
])
Section::make('Adresgegevens')
->collapsible()
->columns(2)
->relationship('location')
->afterStateUpdated(function (?array $state, ?array $old, Set $set) {
if ($state['country_code'] !== 'NL') {
return;
}

if ($state['postal_code'] === $old['postal_code']
&& $state['number'] === $old['number']) {
return;
}

$locationData = LocationData::from([
'postal_code' => $state['postal_code'],
'number' => $state['number'],
'addition' => $state['addition'],
])->updateUsingAPI()->toArray();

$set('location.street', $locationData['street'] ?? null);
$set('location.city', $locationData['city'] ?? null);
$set('location.municipality', $locationData['municipality'] ?? null);
$set('location.province', $locationData['province'] ?? null);
$set('location.lat', $locationData['lat'].'' ?? null);
$set('location.long', $locationData['long'].'' ?? null);
})
->schema([
...
])
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.
Jump to solution
7 Replies
Dennis Koch
Dennis Koch2mo ago
Does it work if you manually fill the form? Are the field $fillable?
AthroHiro
AthroHiro2mo ago
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.
Dennis Koch
Dennis Koch2mo ago
No. I mean by typing on your keyboard 😅
Solution
AthroHiro
AthroHiro2mo ago
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.
AthroHiro
AthroHiro2mo ago
I just checked the docs and it seems like I should use ->readOnly() instead of ->disabled() ...
Dennis Koch
Dennis Koch2mo ago
disabledfields aren't sent to the backend as per HTML spec.
AthroHiro
AthroHiro2mo ago
I understand but the look of a disabled field is sometimes wanted eventhough it's a readonly field.
Want results from more Discord servers?
Add your server