I'v staked with a one thing in laravel. I have huge database with those kinds of tables: products, categories and regions.
Products
- id
- category_id
- region_id
Categories
- id
- parent_id = category.id or 0 if it is root
Region
- id
- parent_id = region.id or 0 if it is root
And I have to get all root regions who connected to specific category. The only solution I see it's to do it like this
$products = Category::products->all();
$rootCategories = [];
foreach($products as $product){
$rootCategories[] = $product->region->ultimateParent(); //Region::ultimateParent();
}
What do you think is there some kind of more eloquent way to solve it?