Reputation: 155
When I have an instance of Eloquent model called $model I can retrieve a value of its attribute (for example name of an user) in two ways:
$name = $user->name;
or
$name = $user->getAttributeValue('name');
Which way is correct and what is the difference between those two ways of retrieving a value?
Upvotes: 3
Views: 720
Reputation: 7194
When you call a model attribute,
__get()
method is called.getAttribute()
method is called.getAttributeValue()
or getRelationValue()
is called.So, it's basically the same using a property or getAttributeValue()
to get a model attribute. Just keep in mind that it can't access model relationships.
Let's note that most (or all?) of the Laravel documentation uses properties to access a model attributes.
Upvotes: 2