/filament-select-tree

The multi-level select field lets you pick one or multiple options from a list that is neatly organized into different levels.

Primary LanguagePHPMIT LicenseMIT

Filament Select Tree

Latest Version on Packagist Total Downloads

This is a package that allows you to create an interactive select tree field based on relationships in your Laravel / Filament application. It provides a convenient way to build hierarchical selection dropdowns with various customization options.

Select Tree

Installation

You can install the package via composer:

composer require codewithdennis/filament-select-tree
php artisan filament:assets

Usage

Import the SelectTree class from the CodeWithDennis\FilamentSelectTree namespace

use CodeWithDennis\FilamentSelectTree\SelectTree;

Create a tree based on a 'BelongsToMany' relationship

SelectTree::make('categories')
    ->relationship('categories', 'name', 'parent_id', function ($query) {
        return $query;
    })

Create a tree based on a 'BelongsTo' relationship

SelectTree::make('category_id')
    ->relationship('category', 'name', 'parent_id', function ($query) {
        return $query;
    })

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;
->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());
        })
])

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])
->disabledOptions(function () {
    return Category::where('is_disabled', true)
        ->get()
        ->pluck('id')
        ->toArray();
})

Screenshots

light dark

Changelog

Please see CHANGELOG for more information on what has changed recently.

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.