Eloquent hasManyThrough: jumping deeper into relationships

Quite often in bigger projects there are DB relationships with more than one level: categories->companies->products, countries->teams->players and similar. If we need to get the list of a deeper level, there’s a quick way of doing that with Eloquent.

Let’s take one of the examples above: product categories, each with companies, each company with products. What if we want to get the list of products by category?

Simplified DB structure:
– id
– name

– id
– category_id
– name

– id
– company_id
– name

So, in our Category model we should probably have companies() with hasMany(‘App\Company’) relationship, and in the model Company there should be products() method with another hasMany(‘App\Product’) relationship. That’s fine, but let’s go a level deeper – meet function hasManyThrough().

Now, if in the code you call $category->products – you will have a perfectly valid list of Product instances.

Please notice the order of parameters – first parameter is the final deepest third-level class, and then comes the middle-part class.

Another quick tip – if you need to specify columns for the relationships, you can do that by adding more parameters at the end:

Check out my new online course: Laravel Eloquent: Expert Level

One thought on “Eloquent hasManyThrough: jumping deeper into relationships

  1. Is there a way to have a hasManyThrough with a Many-to-Many relationship?

    A course can have many modules, a module belongs to a course.
    A module can have many lessons, but lessons can be in many modules.
    How would you write that a Course has many lessons through modules?

Leave a Reply

Your email address will not be published. Required fields are marked *