Black Friday: coupon FRIDAY24 for 40% off Yearly/Lifetime membership! Read more here

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: categories - id - name companies - id - category_id - name products - 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().
class Category extends Model
{
    /**
     * Get all of the products for the category.
     */
    public function products()
    {
        return $this->hasManyThrough('App\Product', 'App\Company');
    }
}
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:
public function products()
{
  return $this->hasManyThrough('App\Product', 'App\Company',
    'category_id', 'company_id');
}

No comments or questions yet...

Like our articles?

Become a Premium Member for $129/year or $29/month
What else you will get:
  • 67 courses (1172 lessons, total 43 h 18 min)
  • 90 long-form tutorials (one new every week)
  • access to project repositories
  • access to private Discord

Recent New Courses