I know its 3 years ago but did you get a solution?
Feb 13, 2017
3
Level 6
Eager load custom Attributes from $appends array on a model?
Hey guys! I have a model "Projects", with a relation to many "Budgets". And to Calculate how many money I have left for my project I'm using a few custom attributes like these:
public function getExternalBudgetAttribute($value){
return $this->budgets->where('type', 'external')->pluck('value')->sum();
}
public function getInternalBudgetAttribute($value){
return $this->budgets->where('type', 'internal')->pluck('value')->sum();
}
Now all my custom attributes are added to the $appends array of the project model. And when I want to view a project, I call $project->load('budgets') for eager loading. So far so good.
My problem is: as soon as I'm iterating over more projects / accessing one of the custom attributes, Laravel performs A LOT of queries. Even though I eagier loaded my relations, it looks like Laravel is always performing a fresh query.
Any ideas how to prevent that?
Please or to participate in this conversation.