I am using Laravel Eloquent models to model these tables:
users: id
firstName
lastName
password
email
posts: id
user_id
title
content
with Post model having a relation of hasOne
('User'
) and User model having a calculated attribute:
protected $appends = ['name'];
public function getNameAttribute(){
return $this->attributes['name'] = $this->firstName.' '. $this->lastName;
}
I need to use eager loading to get only the name of the author of the post, and not all the columns in the users table.
I tried:
$result = Post::with(
[
'user' => function( $query ){
$query->select('name');
}
]
)->get();
but apparently name
is not a column in the table but only a calculated attribute. so how can I get this done?