So, I'm trying to add an attribute (rating) to a model.
So far I did this:
public function getRatingAttribute()
{
return $this::join('reviews', 'accounts.id', '=' , 'reviews.account_id')
->where('accounts.id', $this->attributes['id'])
->select(DB::raw('SUM(reviews.rating) / COUNT(reviews.id)'))->pluck('rating');
}
But it returns Undefined index: id
The strange is if I do a dd
on $this->attributes
it shows all array attributes, including id
.
What am I doing wrong and how can I get the attribute value?