rootCategory is HasOne relation:
public function rootCategory()
{
return $this->hasOne(PartnerCategory::class, 'partner_id', 'id')->where('partner_category_main', 1);
}
then:
$categories = Partner::restaurants()->with('rootCategory')->get()->pluck('rootCategory')
then I need to know if rootCategory has childs with depth >1 and doing in CategoryResource:
$this->descendants()->whereDepth('>', 1)->exists()
but it making much more queries. How I can to query this same time with rootCategory relation?
I tried with(['rootCategory' => function($q) {return $q->descendants();}]) but it not working
whereDeptha query scope or is itwhere('depth', ...)? Instead of querying the existence inside theCategoryResource, you could load a count. (withCountorwithAggregate).withAggregateis similar towithCount. The query would be the same sincewithCountjust useswithAggregatein the background.withAggregate(['descendants as is_deep_tree' => function ($q) { ... }], '*', 'count');