Couple things:
The relation getChild is a bit confusing since a child is a singular noun. I would personally use the plural noun children which makes sense with a hasMany relation. This also aligns with Laravel's naming conventions.
public function children()
{
return $this->hasMany(Category::class, 'parent_id', 'id');
}
Secondly, you could eager load the relationship with the menu collection so that you're reducing the number of database queries:
view()->composer('*', function($view) {
$view
->with('menus', Category::whereParent_id('0')
->with('children')
->get());
});
Thirdly, what do your categories look like? Looking at your code, I don't see how you would see blank submenus, unless the categories stored don't have relating ids?
