diff --git a/src/Collection.php b/src/Collection.php index 2dd26df..099647a 100644 --- a/src/Collection.php +++ b/src/Collection.php @@ -67,6 +67,10 @@ public function toTree($root = false) if ($node->getParentId() == $root) { $items[] = $node; } + + if($node->isLeaf()){ + unset($node->children); + } } return new static($items); @@ -140,4 +144,4 @@ protected function flattenTree(self $groupedNodes, $parentId) return $this; } -} \ No newline at end of file +} diff --git a/src/NestedSet.php b/src/NestedSet.php index 8ec8e02..2f5ac6a 100644 --- a/src/NestedSet.php +++ b/src/NestedSet.php @@ -35,14 +35,16 @@ class NestedSet * Add default nested set columns to the table. Also create an index. * * @param \Illuminate\Database\Schema\Blueprint $table + * @param string $idColumn */ - public static function columns(Blueprint $table) + public static function columns(Blueprint $table, string $idColumn = 'id') { - $table->unsignedInteger(self::LFT)->default(0); - $table->unsignedInteger(self::RGT)->default(0); - $table->unsignedInteger(self::PARENT_ID)->nullable(); + $table->unsignedBigInteger(self::LFT)->default(0); + $table->unsignedBigInteger(self::RGT)->default(0); + $table->unsignedBigInteger(self::PARENT_ID)->nullable(); $table->index(static::getDefaultColumns()); + $table->foreign(self::PARENT_ID)->references($idColumn)->on($table->getTable())->onDelete('cascade'); } /** @@ -54,6 +56,7 @@ public static function dropColumns(Blueprint $table) { $columns = static::getDefaultColumns(); + $table->dropForeign(self::PARENT_ID); $table->dropIndex($columns); $table->dropColumn($columns); } @@ -80,4 +83,4 @@ public static function isNode($node) return is_object($node) && in_array(NodeTrait::class, (array)$node); } -} \ No newline at end of file +} diff --git a/src/NestedSetServiceProvider.php b/src/NestedSetServiceProvider.php index b4516f7..740ef54 100644 --- a/src/NestedSetServiceProvider.php +++ b/src/NestedSetServiceProvider.php @@ -9,12 +9,12 @@ class NestedSetServiceProvider extends ServiceProvider { public function register() { - Blueprint::macro('nestedSet', function () { - NestedSet::columns($this); + Blueprint::macro('nestedSet', function (string $idColumn = 'id') { + NestedSet::columns($this, $idColumn); }); Blueprint::macro('dropNestedSet', function () { NestedSet::dropColumns($this); }); } -} \ No newline at end of file +}