Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Set columns to unsignedBigInteger & Add foreign key to parent id #375

Open
wants to merge 4 commits into
base: v4
Choose a base branch
from
Open
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
13 changes: 8 additions & 5 deletions src/NestedSet.php
Original file line number Diff line number Diff line change
@@ -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);
}

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