So I load all my budgets like so:
$budgets = auth()->user()->budgets()->get(); // With some eager loading and ordering, not needed for this example.
The problem lies in the way I perform some checks in my view. Take for example this snippet:
@foreach($budgets as $budget)
if($budget->getRemainingAmount() < 1) // getRemainingAmount() performs 6 queries
class="danger"
@endif
@endforeach
Now I have one main issue with the above approach. I don't mind the 6 queries, that's just how it works behind the scenes which is totally fine. The thing is, each time I call the method within the view, the 6 queries are run again, which means they are duplicated over and over.
What I want to do is, for example, is include that method in my query and just assign it to a variable.
$budgets = auth()->user()->budgets()->doSomethingToAssignThatMethodToSomeVariable()->get();
Let's just say for now the method does the following simple thing:
public function getRemainingAmount()
{
return 100;
}
Now, how can I assign the method getRemainingAmount() to a variable called $remaining while performing my query? Or, is there a better way to approach this perhaps? In my view, I just want to be able to change this:
if($budget->getRemainingAmount() < 1)
To this (for example):
if($remaining < 1)
So that I can perform the check multiple times without having to run 6 queries over and over, each time I call the method.
Any thoughts on how to achieve this in a simple manner? I actually have multiple methods that result in the same issue (right now my debugbar says: 66 statements were executed, 41 of which were duplicated, 25 unique). Obviously I want to remove the duplication.