Reputation:
I have 3 tables:
structurals
id
name
indicators
id
name
groupings
id
structural_id
indicator_id
And Result:
#structural name
indicator name
indicator name
#structuralname
indicator name
I've used method hasMany & hasManyThrough but errors.
Upvotes: 0
Views: 965
Reputation: 2730
In this case, an indicator
may belong to one or more structural
. And a structural
can have one or more indicators
.
What this describes is a many-to-many
relationship where groupings
is only a pivot table. So you should be using the belongsToMany
instead:
class Indicator extends Model
{
public function structurals()
{
return $this->belongsToMany(Structural::class, 'groupings');
}
}
class Structural extends Model
{
public function indicators()
{
return $this->belongsToMany(Indicator::class, 'groupings');
}
}
Docs: https://laravel.com/docs/eloquent-relationships#many-to-many
You can also remove the id
column from the groupings
table, as it is unnecessary.
Upvotes: 0