Installation
composer require 15web/filament-tree
Publish the config (if needed):
php artisan vendor:publish --provider="15web\FilamentTree\FilamentTreeServiceProvider"
Model Trait
Add the HasTree trait to your Eloquent model:
use HasTree;
class Category extends Model
{
use HasTree;
// ...
}
Filament Resource Integration
In your Filament resource, add the TreeResource trait:
use HasTreeResource;
class CategoryResource extends Resource
{
use HasTreeResource;
// ...
}
First Use Case
Tree Visualization
TreeTable component for nested displays:
public static function table(Table $table): Table
{
return $table
->columns([
TreeColumn::make('name'),
// Other columns...
]);
}
InfoList entries:
TreeColumn::make('title')
->label('Custom Label')
->badge()
->color('success')
Parent-Child Relationships
parent_id foreign key in your model:
public function parent(): BelongsTo
{
return $this->belongsTo(self::class, 'parent_id');
}
tree() method to query the tree structure:
$rootNodes = Category::tree()->get();
Performance Optimization
Category::with('children')->find($id);
Bulk Actions
public static function table(Table $table): Table
{
return $table
->actions([
Action::make('moveToParent')
->requiresConfirmation()
->action(function (Category $record) {
$record->update(['parent_id' => $record->parent_id ?? null]);
}),
]);
}
Custom Tree Logic
use HasTree;
class Category extends Model
{
use HasTree;
public function getTreeOptions(): array
{
return [
'order_by' => 'sort_order',
'depth_limit' => 3, // Optional: Limit nesting depth
];
}
}
Circular References
parent_id never points to a descendant.depth_limit in getTreeOptions() to cap nesting levels.State Persistence
php artisan cache:clear
Drag-and-Drop Quirks
public function canDrag(Category $record): bool
{
return $record->user()->isAdmin();
}
Filament Version Conflicts
composer require filament/filament:v3.1.x
dd(Category::tree()->toArray());
config/filament-tree.php:
'default_collapsed' => false, // Expand all nodes by default
'enable_drag_drop' => env('TREE_ENABLE_DRAG_DROP', true),
Custom Tree Builder Override the tree builder class:
use HasTree;
class Category extends Model
{
use HasTree;
protected $treeBuilder = \Your\Custom\TreeBuilder::class;
}
Event Hooks Listen for tree events (e.g., node reordering):
event(new TreeReordered($node, $newParentId));
API Integration Expose tree data via API:
Route::get('/api/tree', function () {
return Category::tree()->get();
});
Localization Translate labels dynamically:
TreeColumn::make('name')
->label(__('filament-tree::tree.name')),
How can I help you explore Laravel packages today?