efureev / laravel-trees
Multi-Tree structures for Laravel
Installs: 34 099
Dependents: 3
Suggesters: 0
Security: 0
Stars: 122
Watchers: 3
Forks: 16
Open Issues: 1
Requires
- php: ^8.2|^8.3
- efureev/support: ^4.27
- illuminate/database: ^11.0
- illuminate/events: ^11.0
Requires (Dev)
- fakerphp/faker: ^1.23
- orchestra/testbench: ^v9.0
- phpunit/phpunit: ^11.0
- squizlabs/php_codesniffer: ^3.9
- symfony/var-dumper: ^v7.0
- dev-master
- v5.x-dev
- v5.0.0
- v5.0.0-rc2
- v5.0.0-rc1
- v4.0.0
- v3.8.4
- v3.8.3
- v3.8.2
- v3.8.1
- v3.8.0
- v3.7.2
- v3.7.1
- v3.7.0
- v3.6.2
- v3.6.1
- v3.6.0
- v3.5.3
- v3.5.2
- v3.5.1
- v3.5.0
- v3.4.1
- v3.4.0
- v3.3.3
- v3.3.2
- v3.3.1
- v3.3.0
- v3.2.0
- v3.1.2
- v3.1.1
- v3.1.0
- v3.0.1
- v3.0.0
- v2.5.0
- v2.4.0
- v2.3.2
- v2.3.1
- v2.3.0
- v2.2.0
- v2.1.1
- v2.1.0
- v2.0.2
- v2.0.1
- v2.0.0
- v1.7.5
- v1.7.4
- v1.7.3
- v1.7.2
- v1.7.1
- v1.7.0
- v1.6.4
- v1.6.3
- v1.6.2
- v1.6.1
- 1.6.0
- v1.5.2
- v1.5.1
- v1.5.0
- v1.4.0
- v1.3.6
- v1.3.5
- v1.3.4
- v1.3.3
- v1.3.2
- v1.3.1
- v1.3.0
- v1.2.0
- v1.1.9
- v1.1.8
- v1.1.7
- v1.1.6
- v1.1.5
- v1.1.4
- v1.1.3
- v1.1.2
- v1.1.1
- 1.0.0
- dev-lara11
- dev-next
- dev-php7
This package is auto-updated.
Last update: 2024-11-06 21:21:05 UTC
README
Contents:
Information
This package is Multi-Tree structures (a lot of root-nodes).
What are nested sets?
Nested sets or 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
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
- PHP: 8.2|8.3
- Laravel: ^11.*
It is highly suggested to use database that supports transactions (like MySql's InnoDb, Postgres) to secure a tree from possible corruption.
Installation
To install the package, in terminal:
composer require efureev/laravel-trees
Testing
./vendor/bin/phpunit --testdox
or
composer test
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
Model for Single tree structure:
<?php namespace App\Models; use Fureev\Trees\NestedSetTrait; use Illuminate\Database\Eloquent\Model; class Category extends Model { use NestedSetTrait; }
or with custom base config
<?php namespace App\Models; use Fureev\Trees\{NestedSetTrait,Contracts\TreeConfigurable}; use Fureev\Trees\Config\Base; use Illuminate\Database\Eloquent\Model; class Category extends Model implements TreeConfigurable { use NestedSetTrait; protected static function buildTreeConfig(): Base { return new Base(); } }
or with custom config
protected static function buildTreeConfig(): Base { return Base::make() ->setAttribute('parent', ParentAttribute::make()->setName('papa_id')) ->setAttribute('left', LeftAttribute::make()->setName('left_offset')) ->setAttribute('right', RightAttribute::make()->setName('right_offset')) ->setAttribute('level', LevelAttribute::make()->setName('deeeeep')); }
Model for Multi tree structure and with primary key type uuid
:
<?php namespace App\Models; // use Fureev\Trees\Config\TreeAttribute; use Fureev\Trees\Contracts\TreeConfigurable; use Fureev\Trees\NestedSetTrait; use Fureev\Trees\Config\Base; use Illuminate\Database\Eloquent\Model; class Item extends Model implements TreeConfigurable { use NestedSetTrait; protected $keyType = 'uuid'; protected static function buildTreeConfig(): Base { $config= new Base(true); // $config->parent()->setType('uuid'); <-- `parent type` set up automatically from `$model->keyType` return $config; } /* or: protected static function buildTreeConfig(): Base { return Base(TreeAttribute::make('uuid')->setAutoGenerate(false)); } or: protected static function buildTreeConfig(): Base { return Base::make() ->setAttributeTree(TreeAttribute::make()->setName('big_tree_id')) ->setAttribute('parent', ParentAttribute::make()->setName('pid')) ->setAttribute('left', LeftAttribute::make()->setName('left_offset')) ->setAttribute('right', RightAttribute::make()->setName('right_offset')) ->setAttribute('level', LevelAttribute::make()->setName('deeeeep')); } */ }
Use in migrations:
<?php use Fureev\Trees\Migrate; use Illuminate\Database\Migrations\Migration; class AddTemplates extends Migration { public function up() { Schema::create('trees', function (Blueprint $table) { $table->uuid('id')->primary(); $table->string('title'); Migrate::columns($table, (new Page)->getTreeConfig()); $table->timestamps(); $table->softDeletes(); }); } }
Relationships
Node has following relationships that are fully functional and can be eagerly loaded:
- Node belongs to
parent
- Node has many
children
- Node has many
ancestors
- Node has many
descendantsNew
Creating nodes
Creating root-nodes
When you creating a root-node: If you use ...
- single-mode: you may to create ONLY one root-node.
- multi-mode: it will be insert as root-node and different
tree_id
. Default: increment by one. You may customize this function.
These actions are identical:
// For single-root tree Category::make($attributes)->makeRoot()->save(); Category::make($attributes)->saveAsRoot(); Category::create(['setRoot'=>true,...]); // For multi-root tree. If parent is absent, node set as root. Page::make($attributes)->save();
Creating non-root-nodes
When you creating a non-root node, it will be appended to the end of the parent node.
If you want to make node a child of other node, you can make it last or first child.
In following examples, $parent
is some existing node.
Appending to the specified parent
Add child-node into node. Insert after other children of the parent.
$node->appendTo($parent)->save();
Prepending to the specified parent
Add child-node into node. Insert before other children of the parent.
$node->prependTo($parent)->save();
Insert before parent node
Add child-node into same parent node. Insert before target node.
$node->insertBefore($parent)->save();
Insert after parent node
Add child-node into same parent node. Insert after target node.
$node->insertAfter($parent)->save();
Moving nodes
Move node up in self parent scope
$node->up();
Move node down in self parent scope
$node->down();
Deleting nodes
To delete a node:
$node->delete();
IMPORTANT! if deleting node has children - they will be attach to deleted node parent. This behavior may be changed.
IMPORTANT! Nodes are required to be deleted as models! DO NOT try do delete them using a query like so:
Category::where('id', '=', $id)->delete();
This will break the tree!
SoftDeletes
trait is supported, also on model level.
Also you may to delete all children:
$node->deleteWithChildren();
Retrieving nodes
In some cases we will use an $id
variable which is an id of the target node.
Ancestors and descendants
Ancestors make a chain of parents to the node. Helpful for displaying breadcrumbs to the current category.
Descendants are all nodes in a sub tree, i.e. children of node, children of children, etc.
Both ancestors and descendants can be eagerly loaded.
It's relationships:
ancestors
: AncestorsRelationdescendantsNew
: DescendantsRelationchildren
: HasManyparent
: BelongsTo
// Accessing ancestors $node->ancestors; // Accessing descendants $node->descendantsNew; // Accessing descendants $node->children;
Parent
Get parent node
$node->parent;
Collection of parents
$node->parents($level);
Siblings
Siblings are nodes that have same parent.
// Get all siblings of the node $collection = $node->siblings()->get(); // Get siblings which are before the node $collection = $node->prevSiblings()->get(); // Get siblings which are after the node $collection = $node->nextSiblings()->get(); // Get a sibling that is immediately before the node $prevNode = $node->prevSibling()->first(); // Get a sibling that is immediately after the node $nextNode = $node->nextSibling()->first();
$prevNode = $node->prev()->first(); $nextNode = $node->next()->first();
Nodes queries
Model's helpers
Console Tree
Table::fromModel($root->refresh())->draw();
$collection = Structure::all(); Table::fromTree($collection->toTree()) ->hideLevel() ->setExtraColumns( [ 'title' => 'Label', $root->leftAttribute()->name() => 'Left', $root->rightAttribute()->name() => 'Right', $root->levelAttribute()->name() => 'Deep', ] ) ->draw($output);
Structure::all()->toOutput([],null,'...');
Checking consistency
You can check whether a tree is broken (i.e. has some structural errors):
$bool = Category::isBroken();
It is possible to get error statistics:
$data = Category::countErrors();
It will return an array with following keys:
oddness
- the number of nodes that have wrong set oflft
andrgt
valuesduplicates
- the number of nodes that have samelft
orrgt
valueswrong_parent
- the number of nodes that have invalidparent_id
value that doesn't correspond tolft
andrgt
valuesmissing_parent
- the number of nodes that haveparent_id
pointing to node that doesn't exists
Fixing tree
Since v3.3.1 tree can now be fixed.
For single tree:
Node::fixTree();
For multi tree:
Node::fixMultiTree();