2017 © Pedro Peláez
 

library eloquent-tree

Eloquent Tree is a tree model for Laravel Eloquent ORM.

image

gzero/eloquent-tree

Eloquent Tree is a tree model for Laravel Eloquent ORM.

  • Wednesday, February 15, 2017
  • by AdrianSkierniewski
  • Repository
  • 8 Watchers
  • 107 Stars
  • 16,306 Installations
  • PHP
  • 3 Dependents
  • 0 Suggesters
  • 15 Forks
  • 1 Open issues
  • 15 Versions
  • 6 % Grown

The README.md

eloquent-tree Latest Stable Version Total Downloads Build Status

Eloquent Tree is a tree model for Laravel Eloquent ORM., (*1)

Table of Contents

Features

  • Creating root, children and sibling nodes
  • Getting children
  • Getting descendants
  • Getting ancestor
  • Moving sub-tree
  • Building tree on PHP side

Installation

**Version 1.0 is not compatible with 0.***, (*2)

Version 2.0 - Laravel 5 support, (*3)

Version 2.1 - Laravel 5.1 support, (*4)

Version 3.0 - Laravel 5.3 support, (*5)

Begin by installing this package through Composer. Edit your project's composer.json file to require gzero/eloquent-tree., (*6)

"require": {
    "laravel/framework": "5.3.*",
    "gzero/eloquent-tree": "v3.0.*"
},
"minimum-stability" : "stable"

Next, update Composer from the Terminal:, (*7)

composer update

That's all now you can extend \Gzero\EloquentTree\Model\Tree in your project, (*8)

Migration

Simply migration with all required columns that you could extend by adding new fields, (*9)

Schema::create(
    'trees',
    function (Blueprint $table) {
        $table->increments('id');
        $table->string('path', 255)->nullable();
        $table->integer('parent_id')->unsigned()->nullable();
        $table->integer('level')->default(0);
        $table->timestamps();
        $table->index(array('path', 'parent_id', 'level'));
        $table->foreign('parent_id')->references('id')->on('contents')->onDelete('CASCADE');
    }
);

Example usage

Inserting and updating new nodes

$root       = new Tree(); // New root
$root->setAsRoot();
$child      = with(new Tree())->setChildOf($root); // New child
$sibling    = new Tree();
$sibling->setSiblingOf($child); // New sibling

Getting tree nodes

Leaf - returning root node, (*10)

$leaf->findRoot();

Children - returning flat collection of children. You can use Eloquent query builder., (*11)

$collection = $root->children()->get();
$collection2 = $root->children()->where('url', '=', 'slug')->get();

Ancestors - returning flat collection of ancestors, first is root, last is current node. You can use Eloquent query builder. Of course there are no guarantees that the structure of the tree would be complete if you do the query with additional where, (*12)

$collection = $node->findAncestors()->get();
$collection2 = $node->findAncestors()->where('url', '=', 'slug')->get();

Descendants - returning flat collection of descendants, first is current node, last is leafs. You can use Eloquent query builder. Of course there are no guarantees that the structure of the tree would be complete if you do the query with additional where, (*13)

$collection = $node->findDescendants()->get();
$collection2 = $node->findDescendants()->where('url', '=', 'slug')->get();

Building tree structure on PHP side - if some nodes will be missing, these branches will not be built, (*14)

$treeRoot = $root->buildTree($root->findDescendants()->get())

Getting leaf nodes

Tree::getLeaves();

Map from array

Three new roots, first with descendants, (*15)

 Tree::mapArray(
            array(
                array(
                    'children' => array(
                        array(
                            'children' => array(
                                array(
                                    'children' => array(
                                        array(
                                            'children' => array()
                                        ),
                                        array(
                                            'children' => array()
                                        )
                                    )
                                ),
                                array(
                                    'children' => array()
                                )
                            )
                        ),
                        array(
                            'children' => array()
                        )
                    )
                ),
                array(
                    'children' => array()
                ),
                array(
                    'children' => array()
                )
            )
 );

Rendering tree

You can render tree built by the function buildTree, (*16)

 $html = $root->render(
        'ul',
        function ($node) {
            return '<li>' . $node->title . '{sub-tree}</li>';
        },
        TRUE
        );
 echo $html;

Events

All tree models have additional events: * updatingParent * updatedParent * updatedDescendants, (*17)

You can use them for example to update additional tables, (*18)

Support

If you enjoy my work, please consider making a small donation, so I can continue to maintain and create new software to help other users., (*19)

PayPal, (*20)

The Versions

15/02 2017

dev-master

9999999-dev

Eloquent Tree is a tree model for Laravel Eloquent ORM.

  Sources   Download

MIT

The Requires

  • php >=5.6.0

 

The Development Requires

laravel eloquent

15/02 2017

v3.1.1

3.1.1.0

Eloquent Tree is a tree model for Laravel Eloquent ORM.

  Sources   Download

MIT

The Requires

  • php >=5.6.0

 

The Development Requires

laravel eloquent

15/02 2017

v3.1.0

3.1.0.0

Eloquent Tree is a tree model for Laravel Eloquent ORM.

  Sources   Download

MIT

The Requires

  • php >=5.6.0

 

The Development Requires

laravel eloquent

09/12 2016

v3.0.1

3.0.1.0

Eloquent Tree is a tree model for Laravel Eloquent ORM.

  Sources   Download

MIT

The Requires

  • php >=5.6.0

 

The Development Requires

laravel eloquent

08/10 2016

v3.0.0

3.0.0.0

Eloquent Tree is a tree model for Laravel Eloquent ORM.

  Sources   Download

MIT

The Requires

  • php >=5.6.0

 

The Development Requires

laravel eloquent

13/06 2015

v2.1

2.1.0.0

Eloquent Tree is a tree model for Laravel Eloquent ORM.

  Sources   Download

MIT

The Requires

  • php >=5.5.9

 

The Development Requires

laravel eloquent

07/06 2015

v2.0

2.0.0.0

Eloquent Tree is a tree model for Laravel Eloquent ORM.

  Sources   Download

MIT

The Requires

  • php >=5.4.0

 

The Development Requires

laravel eloquent

26/03 2015

v1.0

1.0.0.0

Eloquent Tree is a tree model for Laravel Eloquent ORM.

  Sources   Download

MIT

The Requires

  • php >=5.4.0

 

The Development Requires

laravel eloquent

01/10 2014

v0.7

0.7.0.0

Eloquent Tree is a tree model for Laravel Eloquent ORM.

  Sources   Download

MIT

The Requires

  • php >=5.3.0

 

The Development Requires

laravel eloquent

23/03 2014

v0.6

0.6.0.0

Eloquent Tree is a tree model for Laravel Eloquent ORM.

  Sources   Download

MIT

The Requires

  • php >=5.3.0

 

The Development Requires

laravel eloquent

19/01 2014

v0.5

0.5.0.0

Eloquent Tree is a tree model for Laravel Eloquent ORM.

  Sources   Download

MIT

The Requires

  • php >=5.3.0

 

The Development Requires

laravel eloquent

13/01 2014

v0.4

0.4.0.0

Eloquent Tree is a tree model for Laravel Eloquent ORM.

  Sources   Download

MIT

The Requires

  • php >=5.3.0

 

The Development Requires

laravel eloquent

05/01 2014

v0.3

0.3.0.0

Eloquent Tree is a tree model for Laravel Eloquent ORM.

  Sources   Download

MIT

The Requires

  • php >=5.3.0

 

The Development Requires

laravel eloquent

24/12 2013

v0.2

0.2.0.0

Eloquent Tree is a tree model for Laravel Eloquent ORM.

  Sources   Download

MIT

The Requires

  • php >=5.3.0

 

The Development Requires

laravel

22/12 2013

v0.1

0.1.0.0

Eloquent Tree is a tree model for Laravel Eloquent ORM.

  Sources   Download

MIT

The Requires

  • php >=5.3.0

 

The Development Requires

laravel