|
| 1 | +# Filament Tree Plugin - AI Coding Guide |
| 2 | + |
| 3 | +This is a Laravel package that provides hierarchical tree management for Filament Admin panels, enabling drag-and-drop tree structures for models like menus and categories. |
| 4 | + |
| 5 | +## Architecture Overview |
| 6 | + |
| 7 | +**Trait-Based Design**: The plugin heavily uses traits to add tree functionality to models and pages: |
| 8 | + |
| 9 | +- `ModelTree` trait: Adds tree behavior to Eloquent models (parent-child relationships, ordering) |
| 10 | +- `TreePageTrait` trait: Adds tree page functionality to Filament resources |
| 11 | +- `InteractWithTree` trait: Core tree interaction logic for widgets and pages |
| 12 | + |
| 13 | +**Key Components**: |
| 14 | + |
| 15 | +- `Tree` component: Main UI component handling tree rendering and drag-drop |
| 16 | +- Actions: `CreateAction`, `EditAction`, `DeleteAction`, `ViewAction` for tree items |
| 17 | +- Commands: Artisan generators for tree pages and widgets |
| 18 | + |
| 19 | +## Model Conventions |
| 20 | + |
| 21 | +**Required Database Structure**: |
| 22 | + |
| 23 | +```php |
| 24 | +// Critical: parent_id MUST default to -1 (not null) |
| 25 | +$table->integer('parent_id')->default(-1)->index(); |
| 26 | +$table->integer('order')->default(0); |
| 27 | +$table->string('title'); // or any display field |
| 28 | +``` |
| 29 | + |
| 30 | +**Model Setup Pattern**: |
| 31 | + |
| 32 | +```php |
| 33 | +use SolutionForest\FilamentTree\Concern\ModelTree; |
| 34 | + |
| 35 | +class Category extends Model |
| 36 | +{ |
| 37 | + use ModelTree; |
| 38 | + |
| 39 | + protected $fillable = ["parent_id", "title", "order"]; |
| 40 | + protected $casts = ['parent_id' => 'int']; |
| 41 | +} |
| 42 | +``` |
| 43 | + |
| 44 | +## Page/Widget Creation Patterns |
| 45 | + |
| 46 | +**Use Artisan Generators**: |
| 47 | + |
| 48 | +- `php artisan make:filament-tree-page CategoryTree --resource=Category` |
| 49 | +- `php artisan make:filament-tree-widget CategoryWidget` |
| 50 | + |
| 51 | +**Action Configuration**: Override these methods to customize tree item actions: |
| 52 | + |
| 53 | +```php |
| 54 | +protected function configureEditAction(EditAction $action): EditAction |
| 55 | +{ |
| 56 | + return $action->slideOver()->modalHeading('Edit Category'); |
| 57 | +} |
| 58 | +``` |
| 59 | + |
| 60 | +## Critical Configuration |
| 61 | + |
| 62 | +**Config Structure** (`config/filament-tree.php`): |
| 63 | + |
| 64 | +- Column name mappings for `order`, `parent`, `title` fields |
| 65 | +- Default parent ID (-1 is required convention) |
| 66 | +- Children key name for relationships |
| 67 | + |
| 68 | +**Asset Registration**: After installation, run `php artisan filament:assets` to register CSS/JS assets. |
| 69 | + |
| 70 | +## Build & Development |
| 71 | + |
| 72 | +**Frontend Build**: |
| 73 | + |
| 74 | +- `npm run dev` - Watch mode for styles and scripts |
| 75 | +- `npm run build` - Production build with minification |
| 76 | +- Uses esbuild for JavaScript, TailwindCSS for styles |
| 77 | + |
| 78 | +**Quality Tools**: |
| 79 | + |
| 80 | +- `composer analyse` - PHPStan static analysis |
| 81 | +- `composer lint` - Laravel Pint code formatting |
| 82 | +- `composer test` - Pest testing framework |
| 83 | + |
| 84 | +## Common Patterns |
| 85 | + |
| 86 | +**Tree Depth Control**: Set `protected static int $maxDepth = 2;` in widgets/pages |
| 87 | + |
| 88 | +**Custom Record Display**: |
| 89 | + |
| 90 | +```php |
| 91 | +public function getTreeRecordTitle(?Model $record = null): string |
| 92 | +{ |
| 93 | + return "[{$record->id}] {$record->title}"; |
| 94 | +} |
| 95 | +``` |
| 96 | + |
| 97 | +**Action Visibility**: Control which actions appear using: |
| 98 | + |
| 99 | +```php |
| 100 | +protected function hasEditAction(): bool { return true; } |
| 101 | +protected function hasDeleteAction(): bool { return false; } |
| 102 | +``` |
| 103 | + |
| 104 | +**Tree Record Icons**: Customize icons for tree items: |
| 105 | + |
| 106 | +```php |
| 107 | +public function getTreeRecordIcon(?Model $record = null): ?string |
| 108 | +{ |
| 109 | + if ($record->parent_id != -1) { |
| 110 | + return null; // no icon for child records |
| 111 | + } |
| 112 | + |
| 113 | + return match ($record->title) { |
| 114 | + 'Categories' => 'heroicon-o-tag', |
| 115 | + 'Products' => 'heroicon-o-shopping-bag', |
| 116 | + 'Settings' => 'heroicon-o-cog', |
| 117 | + default => 'heroicon-o-folder', |
| 118 | + }; |
| 119 | +} |
| 120 | +``` |
| 121 | + |
| 122 | +## Key File Locations |
| 123 | + |
| 124 | +- Models: Add `ModelTree` trait to any hierarchical model |
| 125 | +- Pages: Extend from classes in `src/Pages/` or use trait |
| 126 | +- Widgets: Extend `SolutionForest\FilamentTree\Widgets\Tree` |
| 127 | +- Views: Located in `resources/views/` (customizable) |
| 128 | +- Assets: `resources/css/` and `resources/js/` (built to `resources/dist/`) |
0 commit comments