Description
- Laravel Version: 8.82
- Nova Version: 3.32.0
- PHP Version: 8.0.17
- Database Driver & Version: MySQL 8.0.17
- Operating System and Version: n/a
- Browser type and version: n/a
- Reproduction Repository: https://github.com/###/###
Description:
I have a resource that contains a field which is a cast object in its own right (actually a Point type, which contains two float properties for lat & long) that are mapped into two Number
fields in Nova. When updating, the object is converted to a binary string format by its cast so that it's ready to write to the DB, but this string is exposed in Nova and can't be encoded as JSON (because it's not valid UTF-8), so the update fails with this error:
Unable to encode attribute [original] for model [Laravel\Nova\Actions\ActionEvent] to JSON: Malformed UTF-8 characters, possibly incorrectly encoded.
I don't know why it's exposed here, but it is.
Two Nova resource fields point at this single underlying object's properties, with fillUsing
and resolveUsing
to set and get them:
Number::make('Latitude', 'latitude')
->fillUsing(function ($request, $model, $attribute, $requestAttribute) {
$model->location->latitude = (float)$request->input($requestAttribute);
})
->resolveUsing(function ($value, \App\Models\Location $model) {
return $model->location->latitude;
}),
Number::make('Longitude', 'longitude')
->fillUsing(function ($request, $model, $attribute, $requestAttribute) {
$model->location->longitude = (float)$request->input($requestAttribute);
})
->resolveUsing(function ($value, \App\Models\Location $model) {
return $model->location->longitude;
}),
ActionEvent::forResourceUpdate
makes the resource, but I note that the original
property mentioned in the error is created like this:
'original' => array_intersect_key($model->getRawOriginal(), $model->getDirty()),
getRawOriginal
bypasses casts and exposes the raw binary string, so I'm assuming this is where the problem lies.
Ultimately, it's not clear to me whether this is a Nova issue (that it shouldn't be exposing the uncast value) or a problem in the Point class (that it's not casting in the right place).
I apologise that this is not a clearer bug report because I don't know how it's meant to work, so I can't tell what's deviating from what's expected.