I am trying to get the id of the relations I have setup.
Plate model
public function plateContainer()
{
return $this->belongsTo('App\Models\PlateContainer');
}
PlateContainer Model
public function plate()
{
return $this->hasMany('App\Models\Plate');
}
public function equipmentStatusCode()
{
return $this->hasOne('App\Models\EquipmentStatusCode');
}
EquipmentStatusCode Model
public function plateContainer()
{
return $this->belongsTo('App\Models\PlateContainer');
}
When I try.
$p = Plate::findOrFail(4);
$p->plateContainer->equipmentStatusCode;
return $p;
I get Trying to get property of non-object
. What I am doing wrong?
Try this:
I just tried to create a similar situation. I modified the code likewise:
$p = Plate::findOrFail(4);
$p->equipmentStatusCode->plateContainer;
return $p;
I was able to fetch the result array.
plateContainer
and equipmentStatusCode
are a functions. Use rounded brackets.
Add the following relation to the Plate model:
public function equipmentStatusCode()
{
return $this->hasManyThrough('App\Models\EquipmentStatusCode', 'App\Models\PlateContainer');
}
And try:
$p = Plate::findOrFail(4);
$p->equipmentStatusCode();
return $p;