PHPackages                             sohel/traverse-category - PHPackages - PHPackages  [Skip to content](#main-content)[PHPackages](/)[Directory](/)[Categories](/categories)[Trending](/trending)[Leaderboard](/leaderboard)[Changelog](/changelog)[Analyze](/analyze)[Collections](/collections)[Log in](/login)[Sign up](/register)

1. [Directory](/)
2. /
3. [Utility &amp; Helpers](/categories/utility)
4. /
5. sohel/traverse-category

ActiveLibrary[Utility &amp; Helpers](/categories/utility)

sohel/traverse-category
=======================

Get child from parent category and parent from child category

v1.1.0(3y ago)24MITPHP

Since Aug 28Pushed 3y ago1 watchersCompare

[ Source](https://github.com/s-ahmed007/traverse-category)[ Packagist](https://packagist.org/packages/sohel/traverse-category)[ RSS](/packages/sohel-traverse-category/feed)WikiDiscussions main Synced today

READMEChangelog (2)DependenciesVersions (3)Used By (0)

Traverse Category
=================

[](#traverse-category)

Get child from parent category and parent from child category

---

Every time I build another Laravel app where I need all hierarchical child category ids, I find myself re-using the same query over the years. This package allows you to accurately find child category ids of a parent category. This will speed up the development progress.

Installation
------------

[](#installation)

Installation is straightforward, setup is similar to every other Laravel Package.

#### Install via Composer

[](#install-via-composer)

Begin by pulling in the package through Composer:

```
composer require sohel/traverse-category

```

Usage
-----

[](#usage)

This package is easy to use. It provides a handful of helpful functions. It is auto discovered package. So you just need to install this package and use where you need.

#### \[IMPORTANT\] What this package does NOT do

[](#important-what-this-package-does-not-do)

This does NOT return all of the results in the model's table.

---

### Database Structure

[](#database-structure)

Category table should be like following.
`category_id` and its `parent_id` column must be in the same table.

##### Example:

[](#example)

```
Schema::create('categories', function (Blueprint $table) {
	$table->id();
	$table->string('title');
	$table->bigInteger('parent_id')->nullable();
	.....
	.....
});

```

*Note: change table name and column name as you wish.*

### How This Tool Works

[](#how-this-tool-works)

If you have more than 2 level of categories then you may need this.

##### Example:

[](#example-1)

```
Clothing
  -->Men
	-->Shirt
	-->Pant
	-->Shoe
	  -->Sneakers
	  -->Formal Shoes
	  -->Sandals
	  .....
  -->Women
	-->T-Shirt
	-->Pant
	-->Shoe
	  -->Fashion Boots
	  -->Flip Flops
	  -->Ballet Flats
	  .....
  -->Kids
	-->Shirt
	-->Pant
	-->Shoe
	.....

```

### Child Id

[](#child-id)

---

Now if you need all hierarchical ids of `Clothing` category then ultimately you have to get all category ids under `Clothing`.

We have a method called `getAllChildCategoryIds()` which provides your desired child ids along with that `parent_id`. You just need to call this method with some parameters.
Parametes are: `table_name`, `primary_key_column_name`, `parent_id_column_name`, `parent_categroy_id`, `db_connection` (optional).

*Note: default value of `db_connection` is 'mysql'.*

##### Example:

[](#example-2)

```
use Sohel\TraverseCategory\Facades\TraverseCategory;

class TestClass
{
  public function testFunction()
  {
    $categoy_ids = TraverseCategory::getAllChildCategoryIds('categories', 'id', 'parent_id', 3);

    return $category_ids; //sample output [3, 5, 7, 10, 21, 22, .....]
  }
}

```

This method will provide an array of ids. If nothing found then result will be an empty array.

---

We have another method `getDirectChildCategoryIds()` which will provide only the direct child ids. If you need direct child of `Clothing` category then this will return ids of `men`, `women` and `kids`.
This method also receives parameter like above.

##### Example:

[](#example-3)

```
use Sohel\TraverseCategory\Facades\TraverseCategory;

class TestClass
{
  public function testFunction()
  {
    $categoy_ids = TraverseCategory::getDirectChildCategoryIds('categories', 'id', 'parent_id', 3);

    return $category_ids; //sample output [5, 7, 10]
  }
}

```

---

### Parent Id

[](#parent-id)

To get all hierarchical parent ids we can use `getAllParentCategoryIds()`. Parameters are same as above.

Contact
-------

[](#contact)

I am always on Twitter, and it is a great way to communicate with me. [Check me out on Twitter](https://twitter.com/_sohel664).

###  Health Score

23

—

LowBetter than 26% of packages

Maintenance20

Infrequent updates — may be unmaintained

Popularity6

Limited adoption so far

Community9

Small or concentrated contributor base

Maturity48

Maturing project, gaining track record

 Bus Factor1

Top contributor holds 90% of commits — single point of failure

How is this calculated?**Maintenance (25%)** — Last commit recency, latest release date, and issue-to-star ratio. Uses a 2-year decay window.

**Popularity (30%)** — Total and monthly downloads, GitHub stars, and forks. Logarithmic scaling prevents top-heavy scores.

**Community (15%)** — Contributors, dependents, forks, watchers, and maintainers. Measures real ecosystem engagement.

**Maturity (30%)** — Project age, version count, PHP version support, and release stability.

###  Release Activity

Cadence

Every ~2 days

Total

2

Last Release

1401d ago

### Community

Maintainers

![](https://www.gravatar.com/avatar/21d5b714d3b90e0da83b5ac62c6d366c2583a2a25e323c162e9c91f05429fc69?d=identicon)[s-ahmed007](/maintainers/s-ahmed007)

---

Top Contributors

[![1Sohel](https://avatars.githubusercontent.com/u/32958610?v=4)](https://github.com/1Sohel "1Sohel (9 commits)")[![s-ahmed007](https://avatars.githubusercontent.com/u/78678222?v=4)](https://github.com/s-ahmed007 "s-ahmed007 (1 commits)")

### Embed Badge

![Health badge](/badges/sohel-traverse-category/health.svg)

```
[![Health](https://phpackages.com/badges/sohel-traverse-category/health.svg)](https://phpackages.com/packages/sohel-traverse-category)
```

###  Alternatives

[pantheon-systems/pantheon-hud

Provide situational awareness of the Pantheon platform from within your WordPress dashboard.

177.1k](/packages/pantheon-systems-pantheon-hud)

PHPackages © 2026

[Directory](/)[Categories](/categories)[Trending](/trending)[Changelog](/changelog)[Analyze](/analyze)
