Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

felipeb's avatar

Getting a variable ID

Hi guys, I am new to Laravel so forgive me if I'm not very clear or my question is incomplete.

I am implementing a budget feature inside an application. The budget belongs to a team. Inside the budget, I have a column called spend_amount that is the total amount a certain company have per month to spend.

Right now I'm trying to implement the deduction from the spend_amount everytime someone buys a certain product. This is controlled via a SpendingController. This is the code:


namespace App\Http\Controllers\SuperAdmin;

use App\Budget;
use App\Http\Controllers\Controller;
use App\Repos\Teams;
use App\TeamSpending;
use Carbon\Carbon;
use Illuminate\Http\Request;
use Illuminate\View\Factory as View;

class SpendingController extends Controller
{
    protected $budget;

    protected $carbon;

    protected $spending;

    protected $teams;

    protected $view;

    public function __construct(
        Budget $budget,
        Carbon $carbon,
        TeamSpending $spending,
        Teams $teams,
        View $view
    ) {
        $this->budget = $budget;
        $this->carbon = $carbon;
        $this->spending = $spending;
        $this->teams = $teams;
        $this->view = $view;
    }

    public function index(Request $request)
    {
        $teams = $this->teams
            ->where('name', 'LIKE', '%'.$request->input('search').'%')
            ->paginate(12);
        
        $budget = $this->budget->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,
        ]);
    }
}

When I use $budget = $this->budget->first(); the budget for all teams get the number from the first team on my database. When I use $budget = $this->budget I get a 'No budget' for all teams (that is part of a conditional that says that if a budget doesn't exist, it should display 'No budget'). WHEN i try $budget = $this->budget->id I get an error message saying:

TypeError App\TeamSpending::getTeamSpendForMonth(): Argument #1 ($budget) must be of type App\Budget, null given, called in /var/www/html/app/Http/Controllers/SuperAdmin/SpendingController.php on line 52

I don't understand the error... and now I'm stucked trying to figure out if the way I'm calling the budget in the code is wrong, if I should make a query to get the budget of a team or if the problem might be somewhere else...

Thanks for the help!

0 likes
2 replies
LaryAI's avatar
Level 58

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.

Tray2's avatar

Not sure exactly what you are after, but I wouldn't change the original value. but rather add all the transactions to a transactions table and then calculate the remaining, or have two columns one for original value and one for current value, but I would still keep all transactions in a table.

Please or to participate in this conversation.