Read https://laravel.com/docs/7.x/eloquent-serialization#appending-values-to-json (look for Appending At Run Time)
But it propably works only on a single model.
I'm trying to append an attribute to paginated data during runtime.
More specifically, i have a Reply model and i want to append the following attribute
public function getIsLikedAttribute()
{
return $this->likes()->exists();
}
In the controller i have the following line
$replies = $thread->replies()->paginate(Reply::PER_PAGE);
Then the $replies data are passed in a VUE component.
To each reply, i want to append the is_liked attribute, without using
$appends = ['is_liked']
Because in other views i eager load the Reply model and i don't need the is_liked attribute.
@orest Try it like this
$replies = $thread->replies()->paginate(Reply::PER_PAGE);
$replies->each(function ($reply) {
$reply->append('is_liked');
});
Please or to participate in this conversation.