Reputation: 109
So I have 3 tables
size_sets - id, name
sizes - id, name
size_set_sizes - size_id, size_set_id
I want to define a relationship in size_set
model that would retrieve all sizes
available for that sizeset
Something like:
public function sizes()
{
//define relationship here
}
Method sizes
should retrieve the names from the size
table, through size_set_sizes
table in the size_set
model...
My application is very dynamic and thus I needed to go with this structure. I tried the hasManyThrough
relationship, but couldn't get that to work.
Upvotes: 0
Views: 3733
Reputation: 6345
Use belongsToMany()
relations like:
class Size extends Model
{
public function sizeSets()
{
return $this->belongsToMany(SizeSet::class, 'size_set_sizes');
}
}
class SizeSet extends Model
{
public function sizes()
{
return $this->belongsToMany(Size::class, 'size_set_sizes');
}
}
Then you can do:
$sizeSet = SizeSet::with('sizes')->find($id);
Then $sizeSet->sizes
will return a collection of sizes for that size set.
Upvotes: 1
Reputation: 200
100% use a pivot table
https://laravel.com/docs/5.4/eloquent-relationships
This link will give you all you need
Upvotes: 1
Reputation: 109
I Think I found what I was looking for The answer is a pivot-table
http://laraveldaily.com/pivot-tables-and-many-to-many-relationships/
Upvotes: 0