Ya Basha
Ya Basha

Reputation: 1952

retrieve nested eloquent relations items

I have 4 models connected with hasMany relations as below:

Brand -> hasMany -> Categories -> hasMany -> subCategories -> hasMany -> products

I want to retrieve the categories that their subcategories have one or more products.

I tried querying relationship existence with has and/or whereHas but no luck.

Thanx for your advice,

Upvotes: 0

Views: 26

Answers (1)

mrhn
mrhn

Reputation: 18916

Using has() dot annotation, should do the trick for you.

$categories = Category::has('subCategories.products')->get();

Upvotes: 1

Related Questions