Will Daniels
Will Daniels

Reputation: 125

id attribute in eloquent collection changes when converting to an array

I have a database with uuid's as the id column. I do a query using eloquent and it returns the correct information. I have attached an image below because its a big object to write.

enter image description here

So theres 2 items in this collection, they follow the same format but as you can see the first item has the id field which is the correct uuid.

To print this i am doing

$offers = Offer::list($display, false);
dd($offers);

So offers is the collection.

If i do...

$offers = Offer::list($display, false);
dd($offers->toArray());

I get the following...

enter image description here

The id has magically changed into single integers instead of the uuid string I had before.

Does anyone know why it would be doing this? thanks

Upvotes: 1

Views: 636

Answers (1)

namelivia
namelivia

Reputation: 2735

Set the $incrementing property on your model to false, it is documented here on the primary keys section. By default Laravel assumes that the id field on your model is an incrementing integer, that is why models automatically cast id fields to integers.

Upvotes: 4

Related Questions