PHP code example of ibelousov / advanced-nested-set

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

    

ibelousov / advanced-nested-set example snippets


Category::first()->descendants; // print all nested categories of category

Category::whereHas('parents', function($query) {
    $query->where('name', 'vegetables');
});

    public function up()
    {
        Schema::table('categories', function (Blueprint $table) {
             $table->advancedNestedSet(); // create columns lft,rgt,depth,parent_id,deleted_at
        });
    }

    /**
     * Reverse the migrations.
     *
     * @return void
     */
    public function down()
    {
        Schema::table('categories', function (Blueprint $table) {
             $table->dropAdvancedNestedSet(); // delete columns lft,rgt,depth,parent_id,deleted_at
        });
    }

use \Ibelousov\AdvancedNestedSet\AdvancedNestedSet;

class Category extends Model
{
    use AdvancedNestedSet;

    protected $fillable = [/*...Your columns..*/ 'lft','rgt','parent_id','depth','distance'];
}

    php artisan advanced-nested-set:fix categories

ADVANCED_NESTED_LOCK_NAME="NEW_LOCK_NAME"

ADVANCED_NESTED_LOCK_WAIT=100 # Seconds waiting for atomic blocking(DEFAULT: 30)
ADVANCED_NESTED_LOCK_DELAY=9999999 # Microseconds waiting after blocking(DEFAULT: 10000)

$node = Category::create([...]); // Root node

Category::create(['parent_id' => $node->id]); // Child of Root node

$category->update(['parent_id' => null]);

$category->update(['parent_id' => $parentId]);

$category->moveAfter($category2);

// descendants of category
Category::first()->descendants;

// descendants of category and category itself
Category::first()->descendants_and_self;

// parents of category
Category::first()->parents;

// parents and self category of category
Category::first()->parents_and_self;

// parent of category
Category::first()->parent;

// children of category
Category::first()->children;

// Is nested set correct
Category::isCorrect();

// Get errors
Category::errors();

// Convert to treeStructure
Category::get()->toTree();

// Convert to tree and than to array
Category::get()->toTree()->toArray();