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

lyubamt's avatar

PHP Notice: Trying to get property 'amount' of non-object in Psy Shell code on line 1

Hi guys I have a model employee and transaction. ( Employee hasMany Transaction while Transaction belogsTo Employee


namespace App;

use Illuminate\Database\Eloquent\Model;

class Employee extends Model
{
    //
    //create a relationship between employee and transactions
    public function transactions()
    {
    	return $this->hasMany('App\Transaction');
    }

}
<?php

namespace App;

use Illuminate\Database\Eloquent\Model;

class Transaction extends Model
{
    //
    //create a back relationship with Employee model
    public function employee()
    {
    	return $this->belongsTo('App\Employee');
    }
}

The problem is when accessing the reverse relationship with tinker

Transaction::find(1)->employees->amount; PHP Notice: Trying to get property 'amount' of non-object in Psy Shell code on line 1 => null

Any experience please

0 likes
5 replies
MichalOravec's avatar

Your relationship is employee and not employees

Transaction::find(1)->employee->amount;
lyubamt's avatar

Transaction::find(1)->employee->amount PHP Notice: Trying to get property 'amount' of non-object in Psy Shell code on line 1 => null

still the same .even

Transaction::find(2)->employee['amount'] => null

gives null

MichalOravec's avatar
Level 75

@lyubamt So this works?

Transaction::find(1)->employee->amount;

You had letter sbefore in word employee.

Please or to participate in this conversation.