novius/filament-relation-nested
Filament package to manage Eloquent relations backed by kalnoy/nestedset. Adds a TreeRelationManager for nested tree CRUD in your admin panel, with optional actions like FixTree to repair the nested set structure. Compatible with Laravel 11, Filament 5, PHP 8.2+.
Installation:
composer require novius/filament-relation-nested
php artisan filament:assets
First Use Case:
MenuItem with items relation):
// In your Resource's getRelations()
return [
MenuItemsTreeRelationManager::class,
];
kalnoy/nestedset (e.g., extends \Kalnoy\Nestedset\NodeTrait).TreeRelationManager: Extend this for custom relation managers.TreeColumn: Use in tables to visualize nested-set hierarchy.FixTreeAction: Add to header actions for manual tree repair.Relation Manager Setup:
TreeRelationManager and define $relationship.TextColumn for titles, FixTreeAction for tree fixes).class MenuItemsTreeRelationManager extends TreeRelationManager {
protected static string $relationship = 'items';
// ...
}
Tree Visualization:
TreeColumn::make('_lft') to tables for hierarchical sorting._lft:->paginated(fn($table) => !empty($table->getSortColumn()) && $table->getSortColumn() !== '_lft')
Form Integration:
SelectTree (from CodeWithDennis/filament-select-tree) for parent-child selection in forms:SelectTree::make('parent_id')
->options(MenuItem::all()->pluck('title', 'id'))
->searchable()
Create/Edit Nested Records:
CreateAction/EditAction in TreeRelationManager.parent_id field.Bulk Tree Operations:
FixTreeAction to reset tree structure if corrupted.kalnoy/nestedset Methods:
getChildren(), getAncestors(), or getRoot() in custom queries.TreeColumn to limit depth (e.g., 3 levels) via CSS/JS.SoftDeletes if relying on FixTreeAction.Tree Corruption:
FixTreeAction or run php artisan db:seed with a fresh tree.Performance:
->with(['children' => fn($query) => $query->limit(5)]) to eager-load.Sorting Conflicts:
_lft.paginated() closure as shown in the docs.$item->getDescendantsAndSelf()->toSql(); // Verify hierarchy in logs.
FixTreeAction in a try-catch to log errors:FixTreeAction::make()->action(function() {
try {
$this->model->fixTree();
} catch (\Exception $e) {
Log::error($e);
}
});
defaultSort('_lft') to avoid inconsistent ordering._lft/_rgt for TreeColumn; avoid customizing these fields.FixTreeAction to add logic (e.g., validate before fixing).TreeColumn with AJAX for large datasets (requires JS/CSS overrides).TreeRelationManager::form() to add bulk edit for subtree updates.How can I help you explore Laravel packages today?