KeyValue field breaks json into array of characters
I have a json field in my Eloquent model, when I add it in the form schema like so:
Forms\Components\KeyValue::make('data'),
if I open the data, instead of
I see this:
13 Replies
The weird thing is that creating a new entry works fine. Only the reading is somehow broken
Make sure you are casting the field correctly in your model.
How should it be cast? I have it set as a json in the DB and for everything else it works as intended
cast as
array
in the modelHi, I'm having the same problem, but casting as array does not work, it's still showing wrong.
I'm using it in a pivot with:
class ActionInbound extends MorphPivot
{
protected $table = 'action_inbound';
public $incrementing = false;
public $timestamps = false;
protected $casts = [
'inbound_id' => 'int',
'action_id' => 'int',
'config' => 'array',
'actionable_id' => 'int'
];
Using:
->formatStateUsing(function($state) {
return json_decode($state);
})
Shows it properly, but generates the error:
Filament\Forms\Components\KeyValue::Filament\Forms\Components\{closure}(): Argument #1 ($state) must be of type ?array, stdClass given, called in vendor/filament/support/src/Concerns/EvaluatesClosures.php on line 35
So I think is not the best solution.
Any workaround? ThanksIf it’s cast as an array, then why are you decoding the state. It should already be an array?
It should, but it's not an array, at least it has been printed every character in a row. When I do the json_decode (it's only a test, I don't want to do the json_decode as I think it shouldn't be necessary), then it's printed well but then I can't save it (error ($state) must be of type ?array, stdClass given)
I tested that the pivot it's been loaded, and it is. I don't know why this behaviour
can you replicate the same behaviour on a different model that is not a MorphPivot
In other models that are not a MorphPivot, is working well, even with json casting (array or json works well both). For example I have some relation managers with a relationship with another model and there is no problem, it's shown and saved properly.
I tried with:
class ActionInbound extends Pivot
But there is no change in the behaviour, I have no tried with a different pivotdo you have casts defined on the relationship
I have:
class ActionInbound extends MorphPivot
{
protected $table = 'action_inbound';
public $incrementing = false;
public $timestamps = false;
protected $casts = [
'inbound_id' => 'int',
'action_id' => 'int',
'config' => 'array',
'actionable_id' => 'int'
];
Note: if I remove that 'config' cast, and try to create a new attach, I got error: "Array to string conversion", so the cast is taken into account.
In class Action, I have:
public function inbounds()
{
return $this->belongsToMany(Inbound::class)
->using(ActionInbound::class)
->withPivot('config', 'actionable_type', 'actionable_id', 'choice');
}
As far as I know, I don't have any other casts for such 'config' var.Please open an issue with a reproduction repository and we will investigate the issue
Ok thanks, I'll do it