How to get sub-categories of a specific parent category?

Stefan Bogdanescu
Founder & Senior Architect · 3 October 2026

Getting Sub-Categories of a Specific Parent Category

In many applications, especially e-commerce platforms, it’s common to have a hierarchical category structure where categories can belong to other categories as sub-categories. If you’re working with Laravel, handling such relationships is straightforward and efficient.

Understanding the Relationships in Laravel

Firstly, let me clarify the relationship between categories for those who are new to this concept:

  • A parent category is a category that has one or more child categories.
  • Sub-categories or child categories are categories that belong directly under a parent category.

In Laravel, you can define these relationships using hasMany and belongsTo methods in your Category model. Here’s how:

// Category.php (model file)
namespace App\Models;

use Illuminate\Database\Eloquent\Model;

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

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

In the above code:

  • subCategories method uses hasMany to define that a category can have many sub-categories.
  • parentCategory method uses belongsTo to define which parent a category belongs to.

Fetching Sub-Categories

Now, to fetch all the sub-categories of a specific parent category with an ID of 10:

// Controller or any relevant file where you want to use this code
use App\Models\Category;

// Get the main (parent) category
$mainCategory = Category::find(10);

// Fetch its sub-categories using the defined relationship method
$subCategories = $mainCategory->subCategories;

In the above snippet:

  • Category::find(10) is used to find the parent category with an ID of 10.
  • $mainCategory->subCategories fetches all the child categories of the found main category.

Displaying the Sub-Categories

Finally, you can use a blade template or any view-rendering method to display these sub-categories:

// Blade file (for example, index.blade.php)
@foreach($subCategories as $category)
    <p>{{ $category->name }}</p>
@endforeach

Best Practices and Further Reading

It’s crucial to ensure that your database schema supports this relationship. The parent_id field should be defined in the categories table for proper linking.

If you want a more advanced setup, consider using Laravel’s Tree structures or Nestsoft, which provide better handling of complex category hierarchies.

Remember that proper indexing can significantly improve the performance when dealing with large numbers of categories. Always consider your application’s specific needs.

Conclusion

In summary, to get sub-categories in Laravel, you need to:

  1. Define the relationships in your model.
  2. Fetch the parent category using find or any relevant method.
  3. Use the defined relationship method (in this case, subCategories) to fetch its child categories.
  4. Display these fetched categories in your view.

With proper setup and usage of Laravel’s built-in features, handling category hierarchies is a breeze. This approach ensures that your application remains efficient and maintainable.

Our network