PHP code example of uccello / eloquent-tree

1. Go to this page and download the library: Download uccello/eloquent-tree library. Choose the download type require.

2. Extract the ZIP file and open the index.php.

3. Add this code to the index.php.
    
        
<?php
require_once('vendor/autoload.php');

/* Start to develop here. Best regards https://php-download.com/ */

    

uccello / eloquent-tree example snippets


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');
    }
);

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

$leaf->findRoot();

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

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

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

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

Tree::getLeaves();

 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()
                )
            )
 );

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