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

JBF's avatar
Level 3

Tinker Output when retrieving Eloquent Models

When I retrieve a model or models in tinker it outputs the results to screen.

How is that output generated? The model __toString method calls the toJson method but the tinker output is different from that and different from the toArray() output.

Specifically, I have added an attribute to my appends list. I see that attribute in the output from toArray and toJson but not when tinker outputs the model.

Thanks

0 likes
4 replies
Nakov's avatar

@jbf

Based on the documentation

https://laravel.com/docs/master/eloquent-serialization#appending-values-to-json

The appends column is included whenever you cast the model to an array or JSON, otherwise just the attributes from the model are being printed.

So if you do this:

User::first()->toArray();

// or

User::first()->toJson();

The additional column from the appends array will show as well.

Make sure that when you add something new to the appends array, you will have to restart the Tinker session as well.

JBF's avatar
Level 3

Thanks Nakov. That makes sense but my interest was more about what method is being called by tinker to get the output it shows? which doesn't include the attributes in the appends list. Looking through the code, I can't work it out.

Nakov's avatar

@jbf please share some code so me or anyone else on the forum can try and help a bit more.

JBF's avatar
Level 3

It's ok. I figured it out. It calls the castModel method of the TinkerCaster class. Thanks for your help

Please or to participate in this conversation.