PHP code example of artisanweblab / nested-tree

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

    

artisanweblab / nested-tree example snippets


 $table->integer('parent_id')->nullable();
 $table->integer('nest_left')->nullable();
 $table->integer('nest_right')->nullable();
 $table->integer('nest_depth')->nullable();

use ArtisanWebLab\NestedTree\Database\Traits\NestedTreeTrait;

class Category extends Model
{
    use NestedTreeTrait;

    // You can change the column names used by declaring    
    const PARENT_ID = 'my_parent_column';
    const NEST_LEFT = 'my_left_column';
    const NEST_RIGHT = 'my_right_column';
    const NEST_DEPTH = 'my_depth_column';
}

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

$node->makeRoot();

$node->parent_id = null;
$node->save();

$child1 = $root->children()->create(['name' => 'Child 1']);

$child2 = Category::create(['name' => 'Child 2']);
$child2->makeChildOf($root);

$child1->delete();

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