How to get current categorys root parent category and all its sub category

How to get current categorys root parent category and all its sub category

October 20, 2026 · Illustrated author avatarBy Stefan Bogdanescu
How to get current categorys root parent category and all its sub category

Displaying Category Trees in Laravel: The Parent-Child Relationship

As developers working on e-commerce platforms like Magento or custom-built systems using PHP frameworks such as Laravel, we often need to display category trees on our websites. These trees show the hierarchical structure of categories and their sub-categories, providing a clear navigation path for users.

Understanding the Category Hierarchy

In most e-commerce systems, categories have a parent-child relationship. A top-level category is the root or parent, while lower-level categories are its children or sub-categories. This hierarchy can extend to multiple levels, creating complex trees.

For example, consider the following category structure:

  • Cat1
    • Cat11
      • Cat111
    • Cat12

In this tree:

  • Cat1 is the root parent.
  • Cat11 and Cat12 are direct children of Cat1.
  • Cat111 is a child of Cat11, making it a grandchild of Cat1.

Implementing Category Trees in Laravel

Laravel provides robust tools for managing data relationships, which can be leveraged to build category trees.

Step 1: Define the Relationships in Your Models

First, define the relationships between your categories in their respective models. You can use Laravel’s belongsTo relationship for parent-child associations and hasMany for one-to-many connections.

// Category.php (Model)

class Category extends Model
{
    public function children()
    {
        return $this->hasMany(Category::class, 'parent_id');
    }

    public function parents()
    {
        return $this->belongsTo(Category::class, 'parent_id');
    }
}

Step 2: Fetching the Category Tree

Once your relationships are defined, you can fetch the category tree using recursive querying or eager loading.

Recursive Querying

You can use Laravel’s with method to recursively load all related models. Here’s an example:

// Controller or Blade View
$category = Category::find($id);
$categories = $category->children()->with('children');

In this example, we’re fetching the current category and then loading its children and their children as well.

Eager Loading with Laravel

For better performance, especially with large datasets, eager loading is a more efficient approach. It pre-fetches all related models in a single query.

// Controller or Blade View
$category = Category::find($id);
$categories = $category->children()->with('children');

Step 3: Displaying the Tree

Once you have fetched your category tree, you can display it using nested loops or recursive functions.

Displaying Sub-Categories of a Specific Category

If you want to display all sub-categories of a specific category (let’s call this the “root parent”), you can use the following approach:

// Controller or Blade View
$category = Category::find($id);
$subCategories = $category->children;

In this code, $subCategories will contain an array of all direct children of the current category. You can then recursively fetch their children and so on to build your entire tree.

Conclusion

Displaying category trees in Laravel is a key aspect of creating user-friendly navigation systems for e-commerce websites. By understanding and implementing these relationships and querying techniques, you can efficiently manage and display complex hierarchical data structures.

Remember to optimize your queries by using eager loading or indexing when dealing with large datasets. Always aim for clean, maintainable code that adheres to Laravel’s best practices.

For more detailed guides on managing category trees in Laravel, be sure to check out our comprehensive tutorials at Laravel Company.

Happy coding!

Catalog PHP

Our network