Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 5 additions & 1 deletion src/Collection.php
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down Expand Up @@ -140,4 +144,4 @@ protected function flattenTree(self $groupedNodes, $parentId)
return $this;
}

}
}
13 changes: 8 additions & 5 deletions src/NestedSet.php
Original file line number Diff line number Diff line change
Expand Up @@ -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');
}

/**
Expand All @@ -54,6 +56,7 @@ public static function dropColumns(Blueprint $table)
{
$columns = static::getDefaultColumns();

$table->dropForeign(self::PARENT_ID);
$table->dropIndex($columns);
$table->dropColumn($columns);
}
Expand All @@ -80,4 +83,4 @@ public static function isNode($node)
return is_object($node) && in_array(NodeTrait::class, (array)$node);
}

}
}
6 changes: 3 additions & 3 deletions src/NestedSetServiceProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -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);
});
}
}
}