Skip to content

Commit 3438757

Browse files
committed
Set required fillable attribute for ModelTree and add treeColumns for Blueprint while create a ModelTree table
1 parent ba74ec5 commit 3438757

File tree

5 files changed

+52
-2
lines changed

5 files changed

+52
-2
lines changed

config/filament-tree.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
'column_name' => [
88
'order' => 'order',
99
'parent' => 'parent_id',
10-
'depth' => 'depth',
10+
'title' => 'title',
1111
],
1212
/**
1313
* Tree model default parent key

src/Concern/ModelTree.php

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,13 @@ trait ModelTree
1414
SupportTranslation::handleTranslatable as traitHandleTranslatable;
1515
}
1616

17+
public function initializeModelTree()
18+
{
19+
$this->fillable[] = $this->determineOrderColumnName();
20+
$this->fillable[] = $this->determineParentColumnName();
21+
$this->fillable[] = $this->determineTitleColumnName();
22+
}
23+
1724
/**
1825
* Boot the bootable traits on the model.
1926
*/
@@ -85,7 +92,7 @@ public function determineParentColumnName() : string
8592

8693
public function determineTitleColumnName() : string
8794
{
88-
return 'title';
95+
return Utils::titleColumnName();
8996
}
9097

9198
public static function defaultParentKey()

src/FilamentTreeServiceProvider.php

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,9 @@
33
namespace SolutionForest\FilamentTree;
44

55
use Filament\PluginServiceProvider;
6+
use Illuminate\Database\Schema\Blueprint;
67
use Livewire\Livewire;
8+
use SolutionForest\FilamentTree\Macros\BlueprintMarcos;
79
use Spatie\LaravelPackageTools\Package;
810

911
class FilamentTreeServiceProvider extends PluginServiceProvider
@@ -30,4 +32,16 @@ public function configurePackage(Package $package): void
3032
Commands\MakeTreeWidgetCommand::class,
3133
]);
3234
}
35+
36+
public function boot()
37+
{
38+
parent::boot();
39+
40+
$this->registerBlueprintMacros();
41+
}
42+
43+
protected function registerBlueprintMacros()
44+
{
45+
Blueprint::mixin(new BlueprintMarcos);
46+
}
3347
}

src/Macros/BlueprintMarcos.php

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
<?php
2+
3+
namespace SolutionForest\FilamentTree\Macros;
4+
5+
use Illuminate\Database\Schema\Blueprint;
6+
use SolutionForest\FilamentTree\Support\Utils;
7+
8+
/**
9+
* @see Blueprint
10+
*/
11+
class BlueprintMarcos
12+
{
13+
public function treeColumns()
14+
{
15+
return function (string $titleType = 'string') {
16+
$this->{$titleType}(Utils::titleColumnName());
17+
$this->integer(Utils::parentColumnName())->default(Utils::defaultParentId());
18+
$this->integer(Utils::orderColumnName())->default(0)->index();
19+
};
20+
}
21+
}

src/Support/Utils.php

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,11 +14,19 @@ public static function parentColumnName(): string
1414
return config('filament-tree.column_name.parent', 'parent_id');
1515
}
1616

17+
/**
18+
* @deprecated Since v1.0.1
19+
*/
1720
public static function depthColumnName(): string
1821
{
1922
return config('filament-tree.column_name.depth', 'depth');
2023
}
2124

25+
public static function titleColumnName(): string
26+
{
27+
return config('filament-tree.column_name.title', 'title');
28+
}
29+
2230
public static function defaultParentId(): int
2331
{
2432
return (int) config('filament-tree.default_parent_id', -1);

0 commit comments

Comments
 (0)