"Trying to get property of non-object"
I'm afraid I'm on the Code model. And I need the associated Component ID.
I think your relationship is off, if you have a belongsToMany relationship your Code can have many Component and your Component can have many Code. (hence the pivot table)
if your Code has one/many Component then you need Code->one/hasMany->Component and Component->belongsTo->Code
in both cases you can get the first Component name by
public function getParentNameAttribute()
{
$parent = $this->components()->first();
return $parent->name_en;
}
Hi Zenry. I don't quite get what you mean about the relationship. Are you suggesting I change it?
Components can have many codes, yes. And Codes can belong to more than one Component as well. So I reckon a ManyToMany relationship is in order? Please clairfy.
I tried your code without changing my relationships though, and got a "Trying to get property of non-object" error. Don't really know why because it makes sense.
Sign in to participate in this thread!
The Laravel portal for problem solving, knowledge sharing and community building.
The community