Your relationship is employee and not employees
Transaction::find(1)->employee->amount;
Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.
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
@lyubamt So this works?
Transaction::find(1)->employee->amount;
You had letter sbefore in word employee.
Please or to participate in this conversation.