staudenmeir/laravel-adjacency-list

belongsToManyOfDescendantsAndSelf a not unique

YouMixx opened this issue · 3 comments

belongsToManyOfDescendantsAndSelf returns duplicates.

In my case, there are categories (multi-level) and companies.

When opening a category, I get all companies of a certain category through belongs To Many Of DescendantsAndSelf. But if the company is in the parent and in the child category, it will return twice.

I understand why this is happening. But I think that by default this type of communication should return only unique values.

Hi @YouMixx,
You can get unique companies like this:

$uniqueCompanies = $category->companies()->getQuery()->select('companies.*')->distinct()->get();

Don't you think it should be enabled by default?

It would be a breaking change for existing users to suddenly only return unique results.

Besides that, I wouldn't consider them duplicates. The affected companies are connected to the category on different levels of the hierarchy and are different in that sense. If you have additional columns in the category_company pivot table, they can also be different for "duplicate" companies.