PHPackages                             maso/treeify - 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. maso/treeify

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

maso/treeify
============

Laravel package allows you to convert flat collections of models into hierarchical tree structures, simplifying parent-child relationships management.

v1.0.0(1y ago)125MITPHPPHP 7.4|^8.0

Since Sep 2Pushed 1y ago1 watchersCompare

[ Source](https://github.com/MajdSoubh/Treeify)[ Packagist](https://packagist.org/packages/maso/treeify)[ Docs](https://github.com/majdsoubh/treeify)[ RSS](/packages/maso-treeify/feed)WikiDiscussions main Synced 1mo ago

READMEChangelogDependencies (1)Versions (2)Used By (0)

Treeify
=======

[](#treeify)

The Treeify package allows you to easily convert flat collections of Laravel models into hierarchical tree structures based on self-parent-child relationships.

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

[](#installation)

You can install the package via composer:

```
composer require maso/treeify
```

Usage
-----

[](#usage)

To use the Treeify package, simply apply the HasTree trait to your Eloquent models and utilize the scopeTreeify method to generate a hierarchical tree structure:

```
use Maso\Treeify\Traits\HasTree;

class Category extends Model
{
    use HasTree;

    /**
     * The name of the field that identifies the parent ID.
     *
     * This field is optional. If not specified, the default field name 'parent_id' will be used
     *
     */
    protected $parentFieldName = 'parent_id';
}
```

Assuming you have the following categories in your database:

idnameparent\_id1ElectronicsNULL2Laptops13Desktops14Smartphones15Gaming Laptops2#### Generating a Full Tree for All Categories

[](#generating-a-full-tree-for-all-categories)

```
$tree = Category::treeify();
```

Expected Output

```
[
    {
        "id": 1,
        "name": "Electronics",
        "children": [
            {
                "id": 2,
                "name": "Laptops",
                "children": [
                    {
                        "id": 5,
                        "name": "Gaming Laptops"
                    }
                ]
            },
            {
                "id": 3,
                "name": "Desktops"
            },
            {
                "id": 4,
                "name": "Smartphones"
            }
        ]
    }
]
```

#### Generating a Tree Starting from a Specific Category

[](#generating-a-tree-starting-from-a-specific-category)

```
$category = Category::find(2);
$categoryTree = $category->treeify();
```

Expected Output

```
[
    {
        "id": 2,
        "name": "Laptops",
        "children": [
            {
                "id": 5,
                "name": "Gaming Laptops"
            }
        ]
    }
]
```

#### Generating Trees Including All Sub-Parents

[](#generating-trees-including-all-sub-parents)

```
$tree = Category::treeify(false);
```

Expected Output

This method includes all categories in the hierarchy, including sub-parents

```
[
    {
        "id": 1,
        "name": "Electronics",
        "children": [
            {
                "id": 2,
                "name": "Laptops",
                "children": [
                    {
                        "id": 5,
                        "name": "Gaming Laptops"
                    }
                ]
            },
            {
                "id": 3,
                "name": "Desktops"
            },
            {
                "id": 4,
                "name": "Smartphones"
            }
        ]
    },
    {
        "id": 2,
        "name": "Laptops",
        "children": [
            {
                "id": 5,
                "name": "Gaming Laptops"
            }
        ]
    },
    {
        "id": 3,
        "name": "Desktops"
    },
    {
        "id": 4,
        "name": "Smartphones"
    }
]
```

Changelog
---------

[](#changelog)

Please see the [CHANGELOG](https://github.com/MajdSoubh/Treeify/CHANGELOG.md) for more information about what has changed or updated or added recently.

Security
--------

[](#security)

If you discover any security related issues, please email them first to , if we do not fix it within a short period of time please open a new issue describing your problem.

Credits
-------

[](#credits)

- [Majd Soubh](https://www.linkedin.com/in/majd-soubh/)

###  Health Score

26

—

LowBetter than 43% of packages

Maintenance36

Infrequent updates — may be unmaintained

Popularity8

Limited adoption so far

Community7

Small or concentrated contributor base

Maturity44

Maturing project, gaining track record

 Bus Factor1

Top contributor holds 100% 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

Unknown

Total

1

Last Release

623d ago

### Community

Maintainers

![](https://www.gravatar.com/avatar/6ac86a143722a6dfe87873e375c21594a1498c0acdaf071ce5d835d341000089?d=identicon)[majd](/maintainers/majd)

---

Top Contributors

[![MajdSoubh](https://avatars.githubusercontent.com/u/62104933?v=4)](https://github.com/MajdSoubh "MajdSoubh (20 commits)")

---

Tags

treeifybuild-tree

### Embed Badge

![Health badge](/badges/maso-treeify/health.svg)

```
[![Health](https://phpackages.com/badges/maso-treeify/health.svg)](https://phpackages.com/packages/maso-treeify)
```

###  Alternatives

[illuminate/support

The Illuminate Support package.

583107.1M34.5k](/packages/illuminate-support)[pragmarx/countries

PHP Countries and Currencies

1.9k3.3M18](/packages/pragmarx-countries)[illuminate/events

The Illuminate Events package.

13454.3M1.8k](/packages/illuminate-events)[illuminate/config

The Illuminate Config package.

10842.7M2.2k](/packages/illuminate-config)[illuminate/session

The Illuminate Session package.

9937.4M753](/packages/illuminate-session)[illuminate/pagination

The Illuminate Pagination package.

10532.5M862](/packages/illuminate-pagination)

PHPackages © 2026

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