Eloquent has a convenient feature called Accessors – you can define your own custom fields on top of existing in the database table. But then there’s an Eloquent property $appends – should we use it or not? And what’s the difference?
First – how Accessors work
For those who don’t know or have forgotten: for example, if you have User model and fields first_name and last_name in the DB table, then you can create a function in app\User.php:
function getFullNameAttribute() { return $this->first_name . ' ' . $this->last_name; }
Then you have access to property full_name (in the function name it’s CamelCase, and the property name is with underscores _), for example like this:
echo User::find(1)->full_name;
But here’s the thing – if you just return User object, it won’t contain full_name:
dd(User::find(1)->toJSON());
The result would look something like this:
{ "id":1, "first_name":"Povilas", "last_name":"Korop", "email":"povilas@webcoderpro.com", "created_at":"2015-06-19 08:16:58", "updated_at":"2015-06-19 19:48:09" }
Here’s where $appends comes in
Now this is the trick – in your User model you can add $appends attribute and list the fields that would automatically be appended:
class User extends Model { // ... protected $appends = ['full_name'];
Now that attribute will be automatically added to the previous JSON:
{ "id":1, "first_name":"Povilas", "last_name":"Korop", "email":"povilas@webcoderpro.com", "created_at":"2015-06-19 08:16:58", "updated_at":"2015-06-19 19:48:09", "full_name":"Povilas Korop" }
So, in short – Accessor fields would work just by describing getAbcAttribute() methods, but if you want them to be returned in the list as well, then add them to $appends property.
More about Accessors (and related – Mutators) – in the official documentation.
[…] Quelle: laraveldaily.com […]
This saves me! Thank you!
hi, how can i filter by this append?
Hi, Is there a way to append attributes only when needed?
That means, not on the model level.
Just create multiple model that extends your model, I use this on app that need to show user I have a public user and an auth user
I had a similar requirement which was to NOT append the attributes for certain cases.
To do this I created a function on my model like this
public function noAppends() {
$this->appends = [];
return $this;
}
Then I can do things like this
MyModel::find(1)->noAppends()->toArray()
makeHidden function can do the same things
https://laravel.com/docs/5.3/eloquent-serialization
“split_details”: {
“gross_amount”: 101.4,
“discount”: 0,
“distance_fare”: 32,
“time_fare”: 2.38,
“sub_total”: 101.4,
“tax”: 0,
“total_invoice_unround”: 108.1,
“total_invoice”: 108
},
I want to add inside split_details{} this value like “wallet =0” how to append?
I wasn’t able to use the accessors/append field out of the box till I found your article: http://laraveldaily.com/eloquent-how-to-make-lists-work-for-accessor-fields/. I think you should refer it here.
Thank you for your posts.
Very good explanation, thanks a lot, it helps me to understand passing diffForHumans functionality from my laraval model to my vue component to show a userfriendly date format.
Again, thank you for your post.
Could we have a function that takes a parameter, like this?
function getFullNameAttribute($condition)
If I remember correctly, you can also set a default value on fields here, which can be handy.
Ex: `protected $appends = [‘full_name’ => ‘Jeff Madsen’];`
I used this
Thank you!
[…] – with some if-else statements and similar. With Laravel – you can achieve that with Accessor fields or just looping through results in PHP. But there is a more effective way – to move the […]
Hello Povilas,
how can i do where with custom attributes?