Skip to content

efureev/laravel-trees

Repository files navigation

Laravel Tree Structure

PHP Laravel Package PHP Version Laravel Version Total Downloads License composer.lock available Latest Stable Version Maintainability Test Coverage

A Laravel package for implementing multi-tree hierarchical structures using the Nested Set Model.

Overview

This package supports Multi-Tree structures (multiple root nodes) and allows movement of nodes between trees. It works with various model primary key types: int, uuid, and ulid.

Key Advantages

  • Multi-Tree Support: Manage multiple independent trees within the same table
  • Cross-Tree Operations: Move nodes between different trees with ease
  • Flexible Primary Keys: Works with various key types including int, uuid, and ulid
  • Comprehensive Tree Health Tools: Built-in validation and repair utilities for tree integrity
  • Performance Optimized: Efficiently retrieves hierarchical data with minimal database queries
  • Modern PHP Support: Utilizes modern PHP 8.x features and strict typing
  • Flexible Configuration: Highly customizable attribute naming and behavior
  • Well Documented: Complete documentation with practical examples
  • Thoroughly Tested: Comprehensive test suite ensuring reliability

What are nested sets?

Nested Set Model is an efficient way to store hierarchical data in relational databases:

The nested set model numbers 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 attributes. Querying becomes inexpensive: hierarchy membership can be tested by comparing these numbers. Updating requires renumbering and is therefore expensive.

Ideal Use Cases

NSM shows good performance when:

  • Trees are updated infrequently
  • Fast retrieval of related nodes is needed
  • Building multi-depth menus or category structures

Visualization

html tree console tree

Requirements

  • PHP: 8.2|8.3|8.4
  • Laravel: ^11.*

It is highly recommended to use a database that supports transactions (like PostgreSQL) to protect tree structures from corruption.

Installation

composer require efureev/laravel-trees

Documentation

Testing

./vendor/bin/phpunit --testdox
# or
composer test

About

tree structures with best perfomance

Topics

Resources

License

Stars

Watchers

Forks

Packages

No packages published

Contributors 8

Languages