Trying to get property ‘totalInterestPaid’ of non-object when object is present

I have never come across a issue quite like this. Laravel is saying that I am trying to get a property of a non-object when the variable I am referring to is indeed a object.

 $balanceToPush = DB::table('loan_balances')
            ->where('loansID', $loan->id )

        $balanceToPush->totalPaid = number_format($balanceToPush->totalInterestPaid + $balanceToPush->totalTaxPaid + $balanceToPush->totalPrincipalPaid, 2, '.', ',');


        array_push($loanBalancesArray, $balanceToPush);

The above code fails at the first variable in the number formatter $balanceToPush. I can get rid of the first two and just have, for example, $balanceToPush->totalTaxPaid and it will throw the same error.

However, when I dump the $balanceToPush variable it definitely is a object with the fields I am trying to access:

{#1249 ▼
  +"id": 2
  +"loansID": 2
  +"loansBalance": 9000.0
  +"totalInterestPaid": 1670.0
  +"totalPrincipalPaid": 1000.0
  +"totalTaxPaid": 551.1
  +"created_at": "2021-09-16 10:12:27"
  +"updated_at": "2021-09-16 10:12:27"
  +"totalPaid": "3,221.10"

What I have tried?

At this point, I copy and pasted the exact variables from the dd($balanceToPush) and pasted them into the code. Same issue.

I tried to clear the cash by doing php artisan clear:cache, still the same thing.

The most confusing part of this all, was that it was all working fine throughout the day. I then took a break for a few hours came back and then this error popped up.

All help is appreciated.


Thank you for visiting the Q&A section on Magenaut. Please note that all the answers may not help you solve the issue immediately. So please treat them as advisements. If you found the post helpful (or not), leave a comment & I’ll get back to you as soon as possible.

Method 1

This is not really Laravel that is complaining, this is PHP itself.

Althrough it can be confusing, it is very very very unlikely PHP is making a mistake, and you can assume that at some point, you are indeed trying to get a property from a non object.

One of the common mistakes is to use dd in a loop (don’t know if that’s the case here, but if it is, then you only “test” the first iteration then the script stops).

Try this, I rearranged your code so it is easier to debug:

dump($loan); //the same output as dd, but without stopping the script

$balanceToPush = DB::table('loan_balances')
            ->where('loansID', $loan->id )


//$balanceToPush->totalPaid = number_format($balanceToPush->totalInterestPaid + $balanceToPush->totalTaxPaid + $balanceToPush->totalPrincipalPaid, 2, '.', ',');

array_push($loanBalancesArray, $balanceToPush);

You should get a null output at some point. If it works, try again but uncomment the number_format part.

If you are still getting the same error without any null values in the dumps, can you edit your question with the entire exception trace and a wider portion of your code please?

All methods was sourced from or, is licensed under cc by-sa 2.5, cc by-sa 3.0 and cc by-sa 4.0

0 0 votes
Article Rating
Notify of

Inline Feedbacks
View all comments
Would love your thoughts, please comment.x