PHP code example of indigoram89 / laravel-nested-set
1. Go to this page and download the library: Download indigoram89/laravel-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/ */
indigoram89 / laravel-nested-set example snippets
namespace App\Models;
use Indigoram89\NestedSet\Models\NestedSetModel;
class Category extends NestedSetModel
{
protected $table = 'categories';
protected $fillable = ['name', 'slug'];
}
namespace App\Models;
use Illuminate\Database\Eloquent\Model;
use Indigoram89\NestedSet\Traits\NestedSetTrait;
class Category extends Model
{
use NestedSetTrait;
protected $fillable = ['name', 'slug'];
}
// Переместить слева от узла
$node->moveToLeftOf($target);
// Переместить справа от узла
$node->moveToRightOf($target);
// Сделать дочерним узлом
$node->makeChildOf($parent);
// Получить всех потомков
$descendants = $node->getDescendants();
// Получить всех предков
$ancestors = $node->getAncestors();
// Получить прямых потомков
$children = $node->getChildren();
// Получить родителя
$parent = $node->getParent();
// Получить соседние узлы
$siblings = $node->getSiblings();
// Получить путь до узла
$path = $node->getPath();
// Получить все дерево
$tree = Category::query()->getTree();
// Является ли узел корневым
$node->isRoot();
// Является ли узел листом (без потомков)
$node->isLeaf();
// Проверка целостности дерева
$node->isValidNestedSet();
// Удалить узел с потомками
$node->deleteSubtree();
// Получить корневые узлы
Category::query()->roots()->get();
// Получить листья
Category::query()->leaves()->get();
// Получить узлы определенной глубины
Category::query()->withDepth(2)->get();