PHP code example of encima-io / albero

1. Go to this page and download the library: Download encima-io/albero 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/ */

    

encima-io / albero example snippets




namespace Encima\Albero\Models\Category;

use Encima\Albero\HasNestedSets;
use Illuminate\Database\Eloquent\Model;

class Category extends Model
{
    use HasNestedSets;
}


use Encima\Albero\HasNestedSets;
use Encima\Albero\Models\Category;
use Illuminate\Database\Eloquent\SoftDeletes;

class SoftCategory extends Category
{
    use SoftDeletes, HasNestedSets;

    public static function boot() {
        parent::boot();
        static::restoring(function ($node) {
            $node->shiftSiblingsForRestore();
        });

        static::restored(function ($node) {
            $node->restoreDescendants();
        });
    }
}

use Encima\Albero\HasNestedSets;
use Illuminate\Database\Eloquent\Model;

class Category extends Model
{
    use HasNestedSets;
}

use Encima\Albero\HasNestedSets;
use Illuminate\Database\Eloquent\Model;

class Category extends Model
{
    use HasNestedSets;

    protected $table = 'categories';

    // 'parent_id' column name
    protected $parentColumn = 'parent_column';

    // 'left' column name
    protected $leftColumn = 'left_column';

    // 'right' column name
    protected $rightColumn = 'right_column';

    // 'depth' column name
    protected $depthColumn = 'depth_column';
}

use Illuminate\Support\Facades\Schema;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Database\Migrations\Migration;

class CreateCategoriesTable extends Migration
{
    public function up()
    {
        Schema::create('categories', function (Blueprint $table) {
            $table->increments('id');
            $table->unsignedInteger('parent_id')->nullable();
            $table->unsignedInteger('left')->nullable();
            $table->unsignedInteger('right')->nullable();
            $table->unsignedInteger('depth')->nullable();
            $table->string('name');
            $table->timestamps();
        });
    }

    public function down()
    {
        Schema::dropIfExists('categories');
    }
}

$root = Category::create(['name' => 'Root category']);

$node->makeRoot();

// This works the same as makeRoot()
$node->parent_id = null;
$node->save();

// Directly with a relation
$child1 = $root->children()->create(['name' => 'Child 1']);

// with the `makeChildOf` method
$child2 = Category::create(['name' => 'Child 2']);
$child2->makeChildOf($root);

$child1->delete();

$node->getLevel() // 0 when root

$root = Creatures::create(['name' => 'The Root of All Evil']);

$dragons = Creatures::create(['name' => 'Here Be Dragons']);
$dragons->makeChildOf($root);

$monsters = new Creatures(['name' => 'Horrible Monsters']);
$monsters->save();

$monsters->makeSiblingOf($dragons);

$demons = Creatures::where('name', '=', 'demons')->first();
$demons->moveToLeftOf($dragons);

$demons->isRoot(); // => false

$demons->isDescendantOf($root) // => true

$parent = $node->parent()->get();

$children = $node->children()->get();

// Query scope which targets all root nodes
Category::roots()

// All leaf nodes (nodes at the end of a branch)
Category:allLeaves()

$firstRootNode = Category::root();

$node = Category::where('name', '=', 'Books')->first();

foreach($node->getDescendantsAndSelf() as $descendant) {
  echo "{$descendant->name}";
}

$node->descendants()->limitDepth(5)->get();

// This will work without depth limiting
// 1. As usual
$node->getDescendants();
// 2. Selecting only some attributes
$other->getDescendants(['id', 'parent_id', 'name']);
...
// With depth limiting
// 1. A maximum of 5 levels of children will be returned
$node->getDescendants(5);
// 2. A max. of 5 levels of children will be returned selecting only some attrs
$other->getDescendants(5, ['id', 'parent_id', 'name']);

protected $orderColumn = 'name';

$tree = Category::where('name', '=', 'Books')->first()->getDescendantsAndSelf()->toHierarchy();

use Encima\Albero\HasNestedSets;
use Illuminate\Database\Eloquent\Model;

class Category extends Model
{
    use HasNestedSets;

    public static function boot() {
        parent::boot();

        static::moving(function($node) {
            // Before moving the node this function will be called.
        });

        static::moved(function($node) {
            // After the move operation is processed this function will be called.
        });
    }
}

use Encima\Albero\HasNestedSets;
use Illuminate\Database\Eloquent\Model;

class Category extends Model
{
    use HasNestedSets;
    protected $scoped = ['company_id'];
}

$root1 = Category::create(['name' => 'R1', 'company_id' => 1]);
$root2 = Category::create(['name' => 'R2', 'company_id' => 2]);

$child1 = Category::create(['name' => 'C1', 'company_id' => 1]);
$child2 = Category::create(['name' => 'C2', 'company_id' => 2]);

$child1->makeChildOf($root1);
$child2->makeChildOf($root2);

$root1->children()->get(); // <- returns $child1
$root2->children()->get(); // <- returns $child2

Category::isValidNestedSet()
=> true

Category::rebuild()

Category::rebuild(true);

$categories = [
  ['id' => 1, 'name' => 'TV & Home Theather'],
  ['id' => 2, 'name' => 'Tablets & E-Readers'],
  ['id' => 3, 'name' => 'Computers', 'children' => [
    ['id' => 4, 'name' => 'Laptops', 'children' => [
      ['id' => 5, 'name' => 'PC Laptops'],
      ['id' => 6, 'name' => 'Macbooks (Air/Pro)']
    ]],
    ['id' => 7, 'name' => 'Desktops'],
    ['id' => 8, 'name' => 'Monitors']
  ]],
  ['id' => 9, 'name' => 'Cell Phones']
];

Category::buildTree($categories) // => true

$categories = [
  ['id' => 1, 'name' => 'TV & Home Theather'],
  ['id' => 2, 'name' => 'Tablets & E-Readers'],
  ['id' => 3, 'name' => 'Computers', 'children' => [
    ['id' => 4, 'name' => 'Laptops', 'children' => [
      ['id' => 5, 'name' => 'PC Laptops'],
      ['id' => 6, 'name' => 'Macbooks (Air/Pro)']
    ]],
    ['id' => 7, 'name' => 'Desktops', 'children' => [
      // These will be created
      ['name' => 'Towers Only'],
      ['name' => 'Desktop Packages'],
      ['name' => 'All-in-One Computers'],
      ['name' => 'Gaming Desktops']
    ]]
    // This one, as it's not present, will be deleted
    // ['id' => 8, 'name' => 'Monitors'],
  ]],
  ['id' => 9, 'name' => 'Cell Phones']
];

Category::buildTree($categories); // => true

$children = [
  ['name' => 'TV & Home Theather'],
  ['name' => 'Tablets & E-Readers'],
  ['name' => 'Computers', 'children' => [
    ['name' => 'Laptops', 'children' => [
      ['name' => 'PC Laptops'],
      ['name' => 'Macbooks (Air/Pro)']
    ]],
    ['name' => 'Desktops'],
    ['name' => 'Monitors']
  ]],
  ['name' => 'Cell Phones']
];

$electronics = Category::where('name', '=', 'Electronics')->first();
$electronics->makeTree($children); // => true

$node = Category::where('name', '=', 'Some category I do not want to see.')->first();

$root = Category::where('name', '=', 'Old boooks')->first();
var_dump($root->descendantsAndSelf()->withoutNode($node)->get());
... // <- This result set will not contain $node

public static function getNestedList($column, $key = null, $seperator = ' ');

$nestedList = Category::getNestedList('name');
// $nestedList will contain an array like the following:
// array(
//   1 => 'Root 1',
//   2 => ' Child 1',
//   3 => ' Child 2',
//   4 => '  Child 2.1',
//   5 => ' Child 3',
//   6 => 'Root 2'
// );