[!IMPORTANT] Please note that we will only be updating to version 3.x, excluding any bug fixes.
Filament Tree is a plugin for Filament Admin that creates hierarchical tree management with drag-and-drop functionality. Perfect for building menus, categories, organizational structures, and any nested data relationships.
🎯 Key Features:
🚀 Demo: https://filament-cms-website-demo.solutionforest.net/admin
Credentials: demo@solutionforest.net
/ 12345678
(Auto-reset every hour)
Filament Version | Plugin Version |
---|---|
v3 | 2.x.x |
v4 | 3.x.x |
[!IMPORTANT] We only provide updates for versions 3.x, excluding bug fixes for older versions.
Install the package:
composer require solution-forest/filament-tree
Publish and register assets:
php artisan filament:assets
Publish configuration (optional):
php artisan vendor:publish --tag="filament-tree-config"
For custom themes: Add to your tailwind.config.js
:
@import '<path-to-vendor>/solution-forest/filament-tree/resources/css/jquery.nestable.css';@import '<path-to-vendor>/solution-forest/filament-tree/resources/css/button.css';@import '<path-to-vendor>/solution-forest/filament-tree/resources/css/custom-nestable-item.css';@source '<path-to-vendor>/solution-forest/filament-tree/resources/**/*.blade.php';
Create your migration with the required tree structure:
Schema::create('categories', function (Blueprint $table) { $table->id(); $table->treeColumns(); // Adds parent_id, order, title columns $table->timestamps();});Â // Or manually:Schema::create('categories', function (Blueprint $table) { $table->id(); $table->integer('parent_id')->default(-1)->index(); // Must default to -1! $table->integer('order')->default(0); $table->string('title'); $table->timestamps();});
Add the ModelTree
trait to your Eloquent model:
<?php namespace App\Models; use Illuminate\Database\Eloquent\Model;use SolutionForest\FilamentTree\Concern\ModelTree; class Category extends Model{ use ModelTree; protected $fillable = ['parent_id', 'title', 'order']; protected $casts = [ 'parent_id' => 'integer' ];}
Choose your implementation:
# For a standalone tree widgetphp artisan make:filament-tree-widget CategoryWidget --model=Category # For a tree pagephp artisan make:filament-tree-page CategoryTree --model=Category # For a resource tree pagephp artisan make:filament-tree-page CategoryTree --resource=Category
Perfect for embedding trees within existing resource pages or dashboard.
1. Generate the widget:
php artisan make:filament-tree-widget CategoryWidget --model=Category
2. Configure the widget:
<?php namespace App\Filament\Widgets; use App\Models\Category;use Filament\Forms\Components\TextInput;use SolutionForest\FilamentTree\Widgets\Tree as BaseWidget; class CategoryWidget extends BaseWidget{ protected static string $model = Category::class; protected static int $maxDepth = 3; protected ?string $treeTitle = 'Categories'; protected bool $enableTreeTitle = true; protected function getFormSchema(): array { return [ TextInput::make('title')->required(), // Add more form fields as needed ]; }}
3. Display in resource pages:
// In your resource's ListRecords pageprotected function getHeaderWidgets(): array{ return [CategoryWidget::class];}
Standalone pages dedicated to tree management.
1. Generate the page:
php artisan make:filament-tree-page CategoryTree --model=Category
2. Register the page:
// In your PanelProviderpublic function panel(Panel $panel): Panel{ return $panel ->pages([ CategoryTree::class, ]);}
Integrated tree pages within Filament resources.
1. Generate for resource:
php artisan make:filament-tree-page CategoryTree --resource=Category
2. Register in resource:
// In your CategoryResourcepublic static function getPages(): array{ return [ 'index' => Pages\ListCategories::route('/'), 'create' => Pages\CreateCategory::route('/create'), 'edit' => Pages\EditCategory::route('/{record}/edit'), 'tree' => Pages\CategoryTree::route('/tree'), // Add this line ];}
Customize how records appear in the tree:
Custom record titles:
public function getTreeRecordTitle(?\Illuminate\Database\Eloquent\Model $record = null): string{ if (!$record) return '';Â return "[{$record->id}] {$record->title}";}
Record icons:
public function getTreeRecordIcon(?\Illuminate\Database\Eloquent\Model $record = null): ?string{ if ($record->parent_id != -1) { return null; // No icon for child records }Â return match ($record->title) { 'Categories' => 'heroicon-o-tag', 'Products' => 'heroicon-o-shopping-bag', 'Settings' => 'heroicon-o-cog', default => 'heroicon-o-folder', };}
Configure actions that appear for each tree record:
Quick setup with boolean methods:
protected function hasDeleteAction(): bool { return true; }protected function hasEditAction(): bool { return true; }protected function hasViewAction(): bool { return false; }
Advanced action configuration:
protected function configureEditAction(EditAction $action): EditAction{ return $action ->slideOver() ->modalHeading('Edit Category') ->modalSubmitActionLabel('Save Changes');}Â protected function configureDeleteAction(DeleteAction $action): DeleteAction{ return $action ->requiresConfirmation() ->modalDescription('This will permanently delete the category and all subcategories.');}Â protected function configureViewAction(ViewAction $action): ViewAction{ return $action ->slideOver() ->modalWidth('2xl');}
Add global actions displayed above the tree (v3.1.0+):
protected function getTreeToolbarActions(): array{ return [ \SolutionForest\FilamentTree\Actions\CreateAction::make() ->label('Add Category') ->icon('heroicon-o-plus'), \Filament\Actions\ExportAction::make() ->label('Export Tree'), \Filament\Actions\ImportAction::make() ->label('Import Categories'), ];}
Note: Toolbar actions are only supported in version 3.1.0 and later.
Tree depth control:
protected static int $maxDepth = 4; // Limit nesting depth
Node collapsed state:
public function getNodeCollapsedState(?\Illuminate\Database\Eloquent\Model $record = null): bool{ return true; // Start with all nodes collapsed}
Define forms for different operations:
// Used for all operations (fallback)protected function getFormSchema(): array{ return [ TextInput::make('title')->required(), Textarea::make('description'), ];}Â // Specific schemas for different actionsprotected function getCreateFormSchema(): array { /* ... */ }protected function getEditFormSchema(): array { /* ... */ }protected function getViewFormSchema(): array { /* ... */ }
Integration with Spatie Laravel Translatable:
1. Setup your model:
use Filament\Actions\LocaleSwitcher;use SolutionForest\FilamentTree\Concern\ModelTree;use Spatie\Translatable\HasTranslations;Â class Category extends Model{ use HasTranslations, ModelTree;Â protected $translatable = ['title'];}
2. Configure your tree page:
use SolutionForest\FilamentTree\Concern\TreeRecords\Translatable;Â class CategoryTree extends TreePage{ use Translatable;Â public function getTranslatableLocales(): array { return ['en', 'fr', 'es']; }Â protected function getActions(): array { return [LocaleSwitcher::make()]; }}
For advanced use cases, you may want to limit or customize the data displayed in your tree widget: for example, showing only items matching specific criteria or related to a parent resource.
You can achieve this by overriding the getTreeQuery method in your widget, allowing full control over the Eloquent query used to fetch records.
use App\Models\Menuitem;use Illuminate\Database\Eloquent\Builder;class MenuItemsWidget extends Tree{ // Accessing the current record in the widget public ?Model $record = null;Â protected function getTreeQuery(): Builder { return MenuItem::query() ->where('menu_id', $this->record?->id); // Filter by the current menu ID }
Override default column names if your table structure differs:
class Category extends Model{ use ModelTree;Â public function determineOrderColumnName(): string { return 'sort_order'; // Instead of 'order' }Â public function determineParentColumnName(): string { return 'parent_category_id'; // Instead of 'parent_id' }Â public function determineTitleColumnName(): string { return 'name'; // Instead of 'title' }Â public static function defaultParentKey(): int { return 0; // Instead of -1 }}
Performance optimization for large trees:
// Pre-collapse deep nodes to improve initial loadpublic function getNodeCollapsedState(?\Illuminate\Database\Eloquent\Model $record = null): bool{ return $record && $record->getDepth() > 2;}Â // Custom tree depth per implementationprotected static int $maxDepth = 5;
Conditional record display:
public function getTreeRecordTitle(?\Illuminate\Database\Eloquent\Model $record = null): string{ if (!$record) return ''; $title = $record->title; // Add indicators if ($record->children()->count() > 0) { $title .= " ({$record->children()->count()})"; } if (!$record->is_active) { $title = "🚫 " . $title; } return $title;}
The configuration file config/filament-tree.php
allows you to customize default behavior:
<?php return [ /** * Default column names for tree structure */ 'column_name' => [ 'order' => 'order', 'parent' => 'parent_id', 'title' => 'title', ], /** * Default parent ID for root nodes */ 'default_parent_id' => -1, /** * Default children relationship key */ 'default_children_key_name' => 'children',];
Publish additional resources:
# Publish views for customizationphp artisan vendor:publish --tag="filament-tree-views"Â # Publish translationsphp artisan vendor:publish --tag="filament-tree-translations"
-1
as default for parent_id
- required for proper tree functionalityparent_id
and order
columns for better performanceis_active
or status
columns for soft filtering$maxDepth
for better user experience# Development with watch modenpm run dev # Production buildnpm run build # CSS onlynpm run build:styles # JavaScript onlynpm run build:scripts
# Run all testscomposer test # Code analysiscomposer analyse # Code formattingcomposer lint
See CONTRIBUTING for development guidelines.
See the CHANGELOG for more information on what has changed recently.
See CONTRIBUTING for details.
If you discover any security related issues, please email info+package@solutionforest.net instead of using the issue tracker.
Filament Tree is open-sourced software licensed under the MIT license.
Solution Forest Web development agency based in Hong Kong. We help customers to solve their problems. We Love Open Soruces.
We have built a collection of best-in-class products: