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

edwinjansen's avatar

create custom object array

If I request a model, the result is an nice object array. I can reach the items by using "->"

when I create a cutom collection I have to use [] instead of ->

Is it posible to change a "normal" array into an object array within laravel?

The reason I ask is because I have to change/modify some data in the collection I receive from the database and it is very annoying if you have to use [] and -> by each other.

0 likes
1 reply
TerrePorter's avatar

You could try converting it to json then decoding it back to an object.

$obj = json_decode(json_encode($array));

or you might try just converting it

$obj = (object) $array;

Hope that helps

1 like

Please or to participate in this conversation.