The error message you're encountering is indicating that the $budget variable is null when calling the getTeamSpendForMonth() method in the TeamSpending class. This is because you're not retrieving the budget correctly.
In your code, you're trying to access the id property of the $budget object directly, which is causing the error. Instead, you should retrieve the budget using the first() method, and then access its id property.
To fix this issue, modify the line where you assign the $budget variable as follows:
$budget = $this->budget->first()->id;
This will retrieve the first budget from the database and assign its id to the $budget variable.
Here's the updated code:
public function index(Request $request)
{
$teams = $this->teams
->where('name', 'LIKE', '%'.$request->input('search').'%')
->paginate(12);
$budget = $this->budget->first()->id;
$month = $request->query('month') ?? $this->carbon->now()->month;
$year = $request->query('year') ?? $this->carbon->now()->year;
$date = $this->carbon->create($year, $month, 1);
$teams->each(function ($team) use ($date, $budget) {
$team->spending = (object) [
'spent_this_month' => $this->spending->getTeamSpendForMonth($budget, $team, $date),
'percent_of_this_months_budget' => $this->spending->getTeamBudgetSpentPercentForMonth($budget, $team, $date),
];
});
return $this->view->make('super-admin.team-spending.index', [
'budget' => $budget,
'teams' => $teams,
'month' => $month,
'year' => $year,
]);
}
By retrieving the first budget and accessing its id property, you should be able to resolve the error and get the correct budget for each team.