PHPackages                             nevadskiy/laravel-tree - 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. [Database &amp; ORM](/categories/database)
4. /
5. nevadskiy/laravel-tree

ActiveLibrary[Database &amp; ORM](/categories/database)

nevadskiy/laravel-tree
======================

Tree-like structure for Eloquent models.

0.6.1(11mo ago)6693.9k↑36.7%7[4 issues](https://github.com/xalaida/laravel-tree/issues)[2 PRs](https://github.com/xalaida/laravel-tree/pulls)3MITPHPPHP ^7.3|^8.0CI passing

Since Jan 8Pushed 11mo ago1 watchersCompare

[ Source](https://github.com/xalaida/laravel-tree)[ Packagist](https://packagist.org/packages/nevadskiy/laravel-tree)[ RSS](/packages/nevadskiy-laravel-tree/feed)WikiDiscussions master Synced 1mo ago

READMEChangelog (10)Dependencies (4)Versions (19)Used By (3)

[![Stand With Ukraine](https://raw.githubusercontent.com/vshymanskyy/StandWithUkraine/main/banner-direct-single.svg)](https://stand-with-ukraine.pp.ua)

🌳 Tree-like structure for Eloquent models
=========================================

[](#-tree-like-structure-for-eloquent-models)

[![PHPUnit](https://camo.githubusercontent.com/9d76fcee3930e10143b1ea8508ab965ab40710502e0c858dd87ab35032c6ae34/68747470733a2f2f696d672e736869656c64732e696f2f6769746875622f616374696f6e732f776f726b666c6f772f7374617475732f6e65766164736b69792f6c61726176656c2d747265652f706870756e69742e796d6c3f6272616e63683d6d6173746572)](https://packagist.org/packages/nevadskiy/laravel-tree)[![Code Coverage](https://camo.githubusercontent.com/c6801f28f8bc73c3ac12d248c77947f027bcc6ad5c8a189d7a6efcfea4059b55/68747470733a2f2f696d672e736869656c64732e696f2f636f6465636f762f632f6769746875622f6e65766164736b69792f6c61726176656c2d747265653f746f6b656e3d39583641515159435041)](https://packagist.org/packages/nevadskiy/laravel-tree)[![Latest Stable Version](https://camo.githubusercontent.com/395d5d2cb72e6852f588829ba579b93fda7ff767d81f62e0f0601f9065ccb97b/68747470733a2f2f696d672e736869656c64732e696f2f7061636b61676973742f762f6e65766164736b69792f6c61726176656c2d74726565)](https://packagist.org/packages/nevadskiy/laravel-tree)[![License](https://camo.githubusercontent.com/c99ca3c2d8c90aa1f515f7788f4f66ce27302b16e3abefb30cad332674d7000d/68747470733a2f2f696d672e736869656c64732e696f2f6769746875622f6c6963656e73652f6e65766164736b69792f6c61726176656c2d74726565)](https://packagist.org/packages/nevadskiy/laravel-tree)

The package provides you with a simple solution that allows you to effortlessly create hierarchical structures for your Eloquent models. It leverages the [materialized path](#materialized-path) pattern to represent the hierarchy of your data. It can be used for a wide range of use cases such as managing categories, nested comments, and more.

🔌 Installation
--------------

[](#-installation)

Install the package via Composer:

```
composer require nevadskiy/laravel-tree
```

✨ How it works
--------------

[](#-how-it-works)

When working with hierarchical data structures in your application, storing the structure using a self-referencing `parent_id` column is a common approach. While it works well for many use cases, it can become challenging when you need to make complex queries, such as finding all descendants of a given node. One of the simples and effective solutions is the [materialized path](#materialized-path) pattern.

### Materialized path

[](#materialized-path)

The "materialized pattern" involves storing the full path of each node in the hierarchy in a separate `path` column as a string. The ancestors of each node are represented by a series of IDs separated by a delimiter.

For example, the categories database table might look like this:

idnameparent\_idpath1Sciencenull12Physics11.23Mechanics21.2.34Thermodynamics21.2.4With this structure, you can easily retrieve all descendants of a node using a SQL query:

```
SELECT * FROM categories WHERE path LIKE '1.%'
```

#### PostgreSQL Ltree extension

[](#postgresql-ltree-extension)

Using the [PostgreSQL ltree](https://www.postgresql.org/docs/current/ltree.html) extension we can go even further. This extension provides an additional `ltree` column type designed specifically for this purpose. In combination with a GiST index it allows executing lightweight and performant queries across an entire tree.

Now the SQL query will look like this:

```
SELECT * FROM categories WHERE path ~ '1.*'
```

🔨 Configuration
---------------

[](#-configuration)

All you have to do is to add a `AsTree` trait to the model and add a `path` column alongside the self-referencing `parent_id` column to the model's table.

Let's get started by configuring a `Category` model:

```
