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

dde-matt's avatar

PHP Class Visibility

If I create a new instance of my Address Eloquent model, at first obviously no model attributes shows in tinker

$address = new Address;

App\Models\Address {#1065}

But then if I add a value to the reference attribute, the reference attribute will show in tinker

$shape->reference = 123;

App\Models\Address {#1074
    reference: 123,
}

But this property actually lives in the protected $attributes array inside the Eloquent model. So, how does it appear here in tinker?

0 likes
5 replies
bugsysha's avatar

Probably __get()/__set() binds it to be visible in thinker. In the end Tinker is Laravel specific REPL solution so it probably comes with few tricks up it's sleeve. Have you tried with plain class (not model) by adding __get()/__set()?

dde-matt's avatar

Yeah I have tried all the magic methods that are in the eloquent Model, I found that having the __isset() magic method allows the Collection where method to work on my custom class which is cool.

I have gone through how the Model setAttribute() method works and it seems to check if you have any mutators and then saves the value to the protected $attributes array.

I have also implemented ArrayAccess interface and the required methods that allow the class to be interacted with like and array.

Also I implemented the Arrayable interface and the toArray() method, which allows a Collection to turn my custom class into an array.

But, still haven't found a way to show the attributes of my custom class in tinker like a Eloquent Model does.

bugsysha's avatar

I don't have time to experiment that much but it looks like you are on a right track. Why is that important?

dde-matt's avatar

No, it's not important, I was just intrigued and curios to know how that worked

Please or to participate in this conversation.