codewithdennis / filament-select-tree
The multi-level select field enables you to make single selections from a predefined list of options that are organized into multiple levels or depths.
Fund package maintenance!
CodeWithDennis
Requires
- php: ^8.1
- filament/forms: ^3.0
- illuminate/contracts: ^10.0|^11.0
- spatie/laravel-package-tools: ^1.15.0
Requires (Dev)
- nunomaduro/collision: ^7.9
- orchestra/testbench: ^8.0|^9.0
- pestphp/pest: ^2.0
- pestphp/pest-plugin-arch: ^2.0
- pestphp/pest-plugin-laravel: ^2.0
- 3.x-dev
- v3.1.45
- v3.1.44
- v3.1.43
- v3.1.42
- v3.1.41
- v3.1.40
- v3.1.39
- v3.1.38
- v3.1.37
- v3.1.36
- v3.1.35
- v3.1.34
- v3.1.33
- v3.1.32
- v3.1.31
- v3.1.30
- v3.1.29
- v3.1.28
- v3.1.27
- v3.1.26
- v3.1.25
- v3.1.24
- v3.1.23
- v3.1.22
- v3.1.21
- v3.1.20
- v3.1.19
- v3.1.18
- v3.1.17
- v3.1.16
- v3.1.15
- v3.1.14
- v3.1.13
- v3.1.12
- v3.1.11
- v3.1.10
- v3.1.9
- v3.1.8
- v3.1.7
- v3.1.6
- v3.1.5
- v3.1.4
- v3.1.3
- v3.1.2
- v3.1.1
- v3.1.0
- v3.0.4
- v3.0.3
- v3.0.2
- v3.0.1
- v3.0.0
This package is auto-updated.
Last update: 2024-11-03 15:47:06 UTC
README
This package adds a dynamic select tree field to your Laravel / Filament application, allowing you to create interactive hierarchical selection dropdowns based on relationships. It's handy for building selection dropdowns with various customization options.
Installation
You can install the package via composer:
composer require codewithdennis/filament-select-tree
php artisan filament:assets
Relationships
Use the tree for a BelongsToMany
relationship
SelectTree::make('categories') ->relationship('categories', 'name', 'parent_id')
Use the tree for a BelongsTo
relationship
SelectTree::make('category_id') ->relationship('category', 'name', 'parent_id')
Custom Query
Customize the parent query
SelectTree::make('categories') ->relationship(relationship: 'categories', titleAttribute: 'name', parentAttribute: 'parent_id', modifyQueryUsing: fn($query) => $query));
Customize the child query
SelectTree::make('categories') ->relationship(relationship: 'categories', titleAttribute: 'name', parentAttribute: 'parent_id', modifyChildQueryUsing: fn($query) => $query));
Methods
Set a custom placeholder when no items are selected
->placeholder(__('Please select a category'))
Enable the selection of groups
->enableBranchNode()
Customize the label when there are zero search results
->emptyLabel(__('Oops, no results have been found!'))
Display the count of children alongside the group's name
->withCount()
Keep the dropdown open at all times
->alwaysOpen()
Set nodes as dependent
->independent(false)
Expand the tree with selected values (only works if field is dependent)
->expandSelected(false)
Set the parent's null value to -1, allowing you to use -1 as a sentinel value (default = null)
->parentNullValue(-1)
All groups will be opened to this level
->defaultOpenLevel(2)
Specify the list's force direction. Options include: auto (default), top, and bottom.
->direction('top')
Display individual leaf nodes instead of the main group when all leaf nodes are selected
->grouped(false)
Hide the clearable icon
->clearable(false)
Activate the search functionality
->searchable();
Disable specific options in the tree
->disabledOptions([2, 3, 4])
Hide specific options in the tree
->hiddenOptions([2, 3, 4])
Allow soft deleted items to be displayed
->withTrashed()
Specify a different key for your model. For example: you have id, code and parent_code. Your model uses id as key, but the parent-child relation is established between code and parent_code
->withKey('code')
Store fetched models for additional functionality
->storeResults()
Now you can access the results in disabledOptions
or hiddenOptions
->disabledOptions(function ($state, SelectTree $component) { $results = $component->getResults(); })
Filters
Use the tree in your table filters. Here's an example to show you how.
use Filament\Tables\Filters\Filter; use Illuminate\Database\Eloquent\Builder; use CodeWithDennis\FilamentSelectTree\SelectTree;
->filters([ Filter::make('tree') ->form([ SelectTree::make('categories') ->relationship('categories', 'name', 'parent_id') ->independent(false) ->enableBranchNode(), ]) ->query(function (Builder $query, array $data) { return $query->when($data['categories'], function ($query, $categories) { return $query->whereHas('categories', fn($query) => $query->whereIn('id', $categories)); }); }) ->indicateUsing(function (array $data): ?string { if (! $data['categories']) { return null; } return __('Categories') . ': ' . implode(', ', Category::whereIn('id', $data['categories'])->get()->pluck('name')->toArray()); }) ])
Screenshots
Contributing
Please see CONTRIBUTING for details.
Security Vulnerabilities
Please review our security policy on how to report security vulnerabilities.
Credits
License
The MIT License (MIT). Please see License File for more information.