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

la-bas's avatar

Empty model becomes [] when converted to string (json)

Why does an empty model become [] when casted to a string?

$p = new Post;
echo $p;
// Outputs []

I can't understand why the model becomes an empty array in json format. I would expect an empty object.

If I, however give the model an attribute during instantiation it behaves as I would predict.

$p = new Post(['slug' => 'example-post']);
echo $p;
// Outputs {"slug":"example-post"}

Can anyone tell me the reason why the object becomes an empty array in the first example?

0 likes
3 replies
la-bas's avatar

But is there a particular reason that an empty Laravel model becomes an empty array when encoded to json?

json_encode(new Post);
//  "[]"

While any other empty object stays an empty object while encoded to json?

json_encode(new \stdClass);
// "{}"

Please or to participate in this conversation.