A powerful Filament PHP package for implementing deep tree nesting functionality with TreeTable and TreeSelect components. This package provides elegant solutions for handling hierarchical data structures in your Filament applications.
- 🌳 Deep Tree Nesting: Support for unlimited levels of parent-child relationships
- 📊 Tree Table: Display hierarchical data in nested table format with expand/collapse functionality
- 🔽 Tree Select: Advanced select component with tree structure visualization
- 🎯 Model Trait: Simple
HasTree
trait to enable tree functionality on any model - âš¡ Performance Optimized: Efficient queries and lazy loading for large datasets
- 🎨 Customizable: Flexible styling and configuration options
You can install the package via composer:
composer require stringke/filament-tree
Important
If you have not set up a custom theme and are using Filament Panels, follow the instructions in the Filament Docs first.
After setting up a custom theme, add the plugin's views to your theme's CSS file:
@source '../../../../vendor/stringke/filament-tree/resources/**/*.blade.php';
<?php
namespace App\Models;
use Illuminate\Database\Eloquent\Model;
use StringKe\FilamentTree\Traits\HasTree;
class Category extends Model
{
use HasTree;
// Optional: customize the parent and children relationship names
protected string $parentColumn = 'parent_id';
}
use StringKe\FilamentTree\Tables\Columns\TreeColumn;
use StringKe\FilamentTree\Tables\TreeTable;
public static function table(Table $table): Table
{
return TreeTable::make($table)
->columns([
TreeColumn::make('name')
->sortable()
->searchable(),
// Other columns...
])
->defaultSort('name')
->defaultExpanded() // Optional: expand all nodes by default
->maxDepth(5); // Optional: limit the display depth
}
use StringKe\FilamentTree\Forms\Components\TreeSelect;
public static function form(Form $form): Form
{
return $form
->schema([
TreeSelect::make('parent_id')
->relationship('parent', 'name')
->label('Parent Category')
->placeholder('Select parent category...')
->searchable()
->nullable()
->preload() // Load all options on mount
->maxDepth(10), // Optional: limit selection depth
// Other fields...
]);
}
The HasTree
trait provides several useful methods:
// Get all ancestors
$category->ancestors();
// Get all descendants
$category->descendants();
// Get root nodes
Category::roots();
// Get tree depth
$category->getDepth();
// Check if node is root
$category->isRoot();
// Check if node is leaf (no children)
$category->isLeaf();
// Build complete tree
Category::tree();
// Get siblings
$category->siblings();
Your model's database table should have:
- A
parent_id
column (nullable, foreign key to same table) - Standard Laravel timestamps (optional but recommended)
Example migration:
Schema::create('categories', function (Blueprint $table) {
$table->id();
$table->string('name');
$table->foreignId('parent_id')->nullable()->constrained('categories')->onDelete('cascade');
$table->integer('sort')->default(0);
$table->timestamps();
});
While this package works out of the box without configuration files, you can customize behavior through model properties and component options:
class Category extends Model
{
use HasTree;
// Customize parent column name
protected string $parentColumn = 'parent_id';
// Customize children relationship name
protected string $childrenRelation = 'children';
// Customize parent relationship name
protected string $parentRelation = 'parent';
}
Both TreeTable
and TreeSelect
components support various customization options through their fluent API.
composer test
Please see CHANGELOG for more information on what has changed recently.
Please see CONTRIBUTING for details.
Please review our security policy on how to report security vulnerabilities.
The MIT License (MIT). Please see License File for more information.