2

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?

3
  • plateContainer isn't a property of $p but a method. That's why you get the error. Commented Nov 2, 2015 at 8:27
  • @Daan How I might go solving this? Commented Nov 2, 2015 at 8:30
  • specify the table structure for tables plates and equipment_status_codes Commented Nov 2, 2015 at 9:24

2 Answers 2

2

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.

Sign up to request clarification or add additional context in comments.

2 Comments

It is my fault. This one worked getting the very first error was because I had nothing in my DB ( I know it's stupid). Thanks everyone for the effort!
Happy that you found the solution. Thanks :)
2

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;

4 Comments

I have tried this already. I am getting Undefined property: Illuminate\Database\Eloquent\Relations\BelongsTo::$equipmentStatusCode
You have to add rounded brackets after equipmentStatusCode. I edited my asnwer.
I gave your updated code a try, Now I get Call to undefined method Illuminate\Database\Query\Builder::equipmentStatusCode()
Oh yes, you have to use hasManyThrough relation. Please try it!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.