PHPackages                             khazhinov/laravel-trees - 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. khazhinov/laravel-trees

ActiveLibrary

khazhinov/laravel-trees
=======================

Multi-Tree structures for Laravel

v3.7.1(3y ago)1267MITPHPPHP ^8.0|^8.1

Since Feb 8Pushed 3y agoCompare

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

READMEChangelogDependencies (8)Versions (69)Used By (0)

[![PHP Laravel Package](https://github.com/efureev/laravel-trees/workflows/PHP%20Laravel%20Package/badge.svg?branch=master)](https://github.com/efureev/laravel-trees/workflows/PHP%20Laravel%20Package/badge.svg?branch=master)[![](https://camo.githubusercontent.com/15d94c7d95398035ffb28090102acbd7cdda67c63e42b38fb58b1d57461f34e1/68747470733a2f2f696d672e736869656c64732e696f2f62616467652f7068702d253545382e30253743253545382e312d626c75652e737667)](https://camo.githubusercontent.com/15d94c7d95398035ffb28090102acbd7cdda67c63e42b38fb58b1d57461f34e1/68747470733a2f2f696d672e736869656c64732e696f2f62616467652f7068702d253545382e30253743253545382e312d626c75652e737667)[![](https://camo.githubusercontent.com/ee21e850b72e87325d6eedb796e99324a5998adbc42842f47c0556793bc9e988/68747470733a2f2f696d672e736869656c64732e696f2f62616467652f4c61726176656c2d253545382e3830253743253545392e32352d7265642e737667)](https://camo.githubusercontent.com/ee21e850b72e87325d6eedb796e99324a5998adbc42842f47c0556793bc9e988/68747470733a2f2f696d672e736869656c64732e696f2f62616467652f4c61726176656c2d253545382e3830253743253545392e32352d7265642e737667)[![Total Downloads](https://camo.githubusercontent.com/b28b4247ce78b0a83e04eb81f645cdc28ccd3fa2139d421046fa2a7e53145ad2/68747470733a2f2f706f7365722e707567782e6f72672f656675726565762f6c61726176656c2d74726565732f646f776e6c6f616473)](https://packagist.org/packages/efureev/laravel-trees)[![License](https://camo.githubusercontent.com/168c7161009eef1cace44070f0f16f331ae2e965a795bfbd5e39ff5c414d554b/68747470733a2f2f706f7365722e707567782e6f72672f656675726565762f6c61726176656c2d74726565732f6c6963656e7365)](https://packagist.org/packages/efureev/laravel-trees)[![composer.lock available](https://camo.githubusercontent.com/455feb0191da47195d175cc5c9fa9dad8ba237cc92718fca5dd51f63822db231/68747470733a2f2f706f7365722e707567782e6f72672f656675726565762f6c61726176656c2d74726565732f636f6d706f7365726c6f636b)](https://packagist.org/packages/efureev/laravel-trees)

[![Latest Stable Version](https://camo.githubusercontent.com/dbb9cafcd80f37ca1075f180906bc94ff8717f1e104dd41e5d718d2b2721426d/68747470733a2f2f706f7365722e707567782e6f72672f656675726565762f6c61726176656c2d74726565732f76657273696f6e)](https://packagist.org/packages/efureev/laravel-trees)

[![Maintainability](https://camo.githubusercontent.com/dd1efccc696f13bc0b6228e7ec9e93c4979147835c1fca9a64723e6deb8a418a/68747470733a2f2f6170692e636f6465636c696d6174652e636f6d2f76312f6261646765732f36396566663030393861646266373238333431642f6d61696e7461696e6162696c697479)](https://codeclimate.com/github/efureev/laravel-trees/maintainability)[![Test Coverage](https://camo.githubusercontent.com/715b40bc40cc31a833ce26bbe849410c8b3df19159c9e5864fb9d3d4aeee39d7/68747470733a2f2f6170692e636f6465636c696d6174652e636f6d2f76312f6261646765732f36396566663030393861646266373238333431642f746573745f636f766572616765)](https://codeclimate.com/github/efureev/laravel-trees/test_coverage)

**Contents:**

- [Theory](#information)
- [Requirements](#requirements)
- [Installation](#installation)
- [Testing](#testing)
- [Documentation](#documentation)
    - [Migrating](#migrating)
    - [Relationships](#relationships)
    - [Creating nodes](#creating-nodes)
    - [Moving nodes](#moving-nodes)
    - [Deleting nodes](#deleting-nodes)
    - [Retrieving nodes](#retrieving-nodes)
    - [Nodes queries](#nodes-queries)
    - [Model's helpers](#models-helpers)
    - [Console Tree](#console-tree)

Information
-----------

[](#information)

This package is Multi-Tree structures (a lot of root-nodes).

[![table image](./docs/assets/tree.png)](./docs/assets/tree.png)

### What are nested sets?

[](#what-are-nested-sets)

Nested sets or [Nested Set Model](http://en.wikipedia.org/wiki/Nested_set_model) is a way to effectively store hierarchical data in a relational table. From wikipedia:

> The nested set model is to number the nodes according to a tree traversal, which visits each node twice, assigning numbers in the order of visiting, and at both visits. This leaves two numbers for each node, which are stored as two attributes. Querying becomes inexpensive: hierarchy membership can be tested by comparing these numbers. Updating requires renumbering and is therefore expensive.

### Applications

[](#applications)

NSM shows good performance when tree is updated rarely. It is tuned to be fast for getting related nodes. It'is ideally suited for building multi-depth menu or categories for shop.

Requirements
------------

[](#requirements)

- PHP: ^8.0
- Laravel: ^8.80 | ^9.2

It is highly suggested to use database that supports transactions (like MySql's InnoDb, Postgres) to secure a tree from possible corruption.

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

[](#installation)

To install the package, in terminal:

```
composer require efureev/laravel-trees

```

Testing
-------

[](#testing)

```
./vendor/bin/phpunit --testdox

```

or

```
composer test

```

Documentation
-------------

[](#documentation)

This package works with different model primary key: `int`, `uuid`. This package allows to creating multi-root structures: no only-one-root! And allow to move nodes between trees.

### Migrating

[](#migrating)

**Model for Single tree structure:**

```
