Support the ongoing development of Laravel.io →
posted 10 years ago
Eloquent
Last updated 2 years ago.
0

$this->id instead of 50?

Last updated 2 years ago.
0

"Trying to get property of non-object"

I'm afraid I'm on the Code model. And I need the associated Component ID.

Last updated 2 years ago.
0

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;
}
Last updated 2 years ago.
0

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.

Last updated 2 years ago.
0

u can use setAttribute function

Last updated 2 years ago.
0

Sign in to participate in this thread!

Eventy

Your banner here too?

Moderators

We'd like to thank these amazing companies for supporting us

Your logo here?

Laravel.io

The Laravel portal for problem solving, knowledge sharing and community building.

© 2024 Laravel.io - All rights reserved.