vendredi 30 décembre 2016

Laravel Relations of model to itself

I am trying to utilize a parent/child relation within one table. Each asset belongs to a parent asset, which is another asset in the assets table/asset model.

Each task in the tasks table has an asset_id record and that asset identified by the ID has a parent_asset_id in its record. I am trying to access the name field of the parent of the asset used in a task and I am getting an error. Here is the code involved:

This is the relation in the asset model:

public function parent_asset()
   {
     return $this->belongsTo('\pmms\Asset', 'parent_asset_id');
   }

and the relation in the task model

  public function asset()
   {
     return $this->belongsTo('\pmms\Asset');
   }

Ina blade file I am trying to access the name field in the asset table like this:

$task->asset->parent_asset->name

but I get the error:

Trying to get property of non-object (View: C:\pmms\resources\views\partials\tasklist.blade.php)

Where am I going wrong here?



from Newest questions tagged laravel-5 - Stack Overflow http://ift.tt/2ifW135
via IFTTT

Aucun commentaire:

Enregistrer un commentaire