Search code examples
laraveleloquentrelationships

Join 3 tables using Laravel Eloquent


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.


Solution

  • 100% use a pivot table

    https://laravel.com/docs/5.4/eloquent-relationships

    This link will give you all you need