All Projects → AdrianSkierniewski → Eloquent Tree

AdrianSkierniewski / Eloquent Tree

Licence: mit
Eloquent Tree is a tree model for Laravel Eloquent ORM.

Projects that are alternatives of or similar to Eloquent Tree

Laravel Server Monitor
Server Monitoring Command for Laravel Applications
Stars: ✭ 424 (+223.66%)
Mutual labels:  laravel, laravel-package, composer
Telegram Bot Sdk
🤖 Telegram Bot API PHP SDK. Lets you build Telegram Bots easily! Supports Laravel out of the box.
Stars: ✭ 2,212 (+1588.55%)
Mutual labels:  laravel, laravel-package, composer
Package Skeleton
📦 My base for PHP packages.
Stars: ✭ 6 (-95.42%)
Mutual labels:  laravel, laravel-package, composer
Jwt Auth Guard
JWT Auth Guard for Laravel and Lumen Frameworks.
Stars: ✭ 319 (+143.51%)
Mutual labels:  laravel, laravel-package, composer
Laravel Stats
📈 Get insights about your Laravel or Lumen Project
Stars: ✭ 1,386 (+958.02%)
Mutual labels:  laravel, laravel-package, composer
Laravel Fpdf
Create PDFs with Laravel, provides FPDF version 1.82
Stars: ✭ 108 (-17.56%)
Mutual labels:  laravel, laravel-package
Laravel Geo Routes
GeoLocation restricted routes for Laravel
Stars: ✭ 110 (-16.03%)
Mutual labels:  laravel, laravel-package
Sms
Laravel SMS Gateway Integration Package
Stars: ✭ 112 (-14.5%)
Mutual labels:  laravel, composer
Pagination
🎁 Laravel 5 Custom Pagination Presenter
Stars: ✭ 119 (-9.16%)
Mutual labels:  laravel, laravel-package
Laravel Enum
Elegant Enum implementation for Laravel
Stars: ✭ 107 (-18.32%)
Mutual labels:  laravel, laravel-package
Eye
Eyewitness.io package for Laravel 5 applications
Stars: ✭ 114 (-12.98%)
Mutual labels:  laravel, laravel-package
Rpg
Online Role Playing Game (based on Laravel)
Stars: ✭ 121 (-7.63%)
Mutual labels:  laravel, composer
Nova Indicator Field
A colour-coded indicator field for Laravel Nova
Stars: ✭ 108 (-17.56%)
Mutual labels:  laravel, laravel-package
Cray
A Laravel package to help you generate nearly complete CRUD pages like crazy!
Stars: ✭ 108 (-17.56%)
Mutual labels:  laravel, laravel-package
Laravel Alert
A Bootstrap alert helper for Laravel
Stars: ✭ 110 (-16.03%)
Mutual labels:  laravel, composer
Laravel Excel
🚀 Supercharged Excel exports and imports in Laravel
Stars: ✭ 10,417 (+7851.91%)
Mutual labels:  laravel, laravel-package
Laravel Natural Language
This package makes using the Google Natural API in your laravel app a breeze with minimum to no configuration, clean syntax and a consistent package API.
Stars: ✭ 119 (-9.16%)
Mutual labels:  laravel, laravel-package
Docker Octobercms
Dockerized October CMS: PHP, Composer, October core and dependencies
Stars: ✭ 125 (-4.58%)
Mutual labels:  laravel, composer
Laravel Meta
Metadata for Eloquent model
Stars: ✭ 124 (-5.34%)
Mutual labels:  laravel, laravel-package
Laravel Short Url
A Laravel package to shorten urls
Stars: ✭ 127 (-3.05%)
Mutual labels:  laravel, laravel-package

eloquent-tree Latest Stable Version Total Downloads Build Status

Eloquent Tree is a tree model for Laravel Eloquent ORM.

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.*

Version 2.0 - Laravel 5 support

Version 2.1 - Laravel 5.1 support

Version 3.0 - Laravel 5.3 support

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

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

Next, update Composer from the Terminal:

composer update

That's all now you can extend \Gzero\EloquentTree\Model\Tree in your project

Migration

Simply migration with all required columns that you could extend by adding new fields

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

$leaf->findRoot();

Children - returning flat collection of children. You can use Eloquent query builder.

$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

$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

$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

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

Getting leaf nodes

Tree::getLeaves();

Map from array

Three new roots, first with descendants

 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

 $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

You can use them for example to update additional tables

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.

PayPal

Note that the project description data, including the texts, logos, images, and/or trademarks, for each open source project belongs to its rightful owner. If you wish to add or remove any projects, please contact us at [email protected].