I have the category model where each category can be general or nesting.
Category: id parent_id title
To get children of category and all nested children of category use relation with recursion
public function children()
{
return $this->hasMany('App\Models\Category', 'parent_id', 'id');
}
public function allChildren()
{
return $this->children()->with('allChildren');
}
And relation for products
public function product()
{
return $this->belongsToMany('App\Models\Product', 'categories_products');
}
How can I get all products from base category and all nested categories?