KeyValue is missing the associative part on edit

I'm using a KeyValue component inside a RelationManager form.
Forms\Components\KeyValue::make('error')
->json()
->disableAddingRows()
->disableDeletingRows()
->disableEditingKeys()
->columnSpan('full')
Forms\Components\KeyValue::make('error')
->json()
->disableAddingRows()
->disableDeletingRows()
->disableEditingKeys()
->columnSpan('full')
My error field contains a json object and is set on a pivot model. the relationship
public function documents(): BelongsToMany
{
return $this->belongsToMany(Document::class)->using(DocumentKyc::class)
->withPivot(columns: ['id', 'result', 'error', 'done_at']);
}
public function documents(): BelongsToMany
{
return $this->belongsToMany(Document::class)->using(DocumentKyc::class)
->withPivot(columns: ['id', 'result', 'error', 'done_at']);
}
On my intermediate model DocumentKyc I cast the column error as an array:
protected $casts = [
'error' => 'array'
];
protected $casts = [
'error' => 'array'
];
When I go to edit my relation the KeyValue field look like the attachment I gave. Note : I've found a solution by mutating my data before fill :
Tables\Actions\EditAction::make()->mutateRecordDataUsing(function ($data) {
$data['error'] = json_decode($data['error'], true);
return $data;
}),
Tables\Actions\EditAction::make()->mutateRecordDataUsing(function ($data) {
$data['error'] = json_decode($data['error'], true);
return $data;
}),
But shouldn't it recognize the associative array by default ? Or maybe I'm missing something obvious here... Anyway, thanks in advance
5 Replies
Unknown User
Unknown User2y ago
Message Not Public
Sign In & Join Server To View
Dan Harrin
Dan Harrin2y ago
that screenshot suggests that the keyvalue is recieving a json string, not an array the problem is that you probably need ->withCasts([‘error’ => ‘array’]) on your relationship?
Tanuki
TanukiOP2y ago
Oh, I didn't know I had to specify the cast on the relationship as well, since I added the $cast on the pivot model itself (I'm gonna try right away)
Dan Harrin
Dan Harrin2y ago
i think its a laravel quirk rather than something on our end
Tanuki
TanukiOP2y ago
Yeah indeed, it works ! As always, thanks a lot 🙂
Want results from more Discord servers?
Add your server