Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

vincent15000's avatar

Problem with array casting

Hello,

I have defined a casting for the config field as an array.

protected $casts = [
	'config' => 'array',
];

When I set the ApiResource, the config property isn't converted to an object and I can't access the properties inside config from VueJS.

return [
	'config' => $this->config,
];
// VueJS

{{ props.config.trunk }} // not working

{{ JSON.parse(props.config).trunk }} // working

To have it working, I need to convert manually the config property.

return [
	'config' => json_decode($this->config, true),
];

And then it works.

Shouldn't the casting do this automatically ?

Thanks for your help ;).

V

0 likes
1 reply
vincent15000's avatar
vincent15000
OP
Best Answer
Level 63

Hmmm ... I think that I understand why it doesn't work.

From VueJS I already get a JSON string, so it's not necessary to cast the field.

Please or to participate in this conversation.