diff --git a/README.md b/README.md index 6d78af3..afb9b8d 100644 --- a/README.md +++ b/README.md @@ -3,7 +3,7 @@ [![Latest Version on Packagist](https://img.shields.io/packagist/v/codewithdennis/filament-select-tree.svg?style=flat-square)](https://packagist.org/packages/codewithdennis/filament-select-tree) [![Total Downloads](https://img.shields.io/packagist/dt/codewithdennis/filament-select-tree.svg?style=flat-square)](https://packagist.org/packages/codewithdennis/filament-select-tree) -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. +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. ![Select Tree](https://github.com/CodeWithDennis/filament-select-tree/assets/23448484/d944b896-134b-414a-b654-9adecc43ba5e) @@ -20,63 +20,27 @@ composer require codewithdennis/filament-select-tree php artisan filament:assets ``` -## Usage +## Relationships -Import the `SelectTree` class from the `CodeWithDennis\FilamentSelectTree` namespace +#### Use the tree for a `BelongsToMany` relationship ```PHP use CodeWithDennis\FilamentSelectTree\SelectTree; -``` - -Create a tree based on a 'BelongsToMany' relationship -```PHP SelectTree::make('categories') - ->relationship('categories', 'name', 'parent_id', function ($query) { - return $query; - }) + ->relationship('categories', 'name', 'parent_id') ``` -Create a tree based on a 'BelongsTo' relationship +#### Use the tree for a `BelongsTo` relationship ```PHP +use CodeWithDennis\FilamentSelectTree\SelectTree; + SelectTree::make('category_id') - ->relationship('category', 'name', 'parent_id', function ($query) { - return $query; - }) + ->relationship('category', 'name', 'parent_id') ``` -Use the tree in your table filters. Here's an example to show you how. - - -```bash -use Filament\Tables\Filters\Filter; -use Illuminate\Database\Eloquent\Builder; -``` - -```php -->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()); - }) -]) -``` +## Methods Set a custom placeholder when no items are selected @@ -162,35 +126,46 @@ Disable specific options in the tree ->disabledOptions([2, 3, 4]) ``` -```PHP -->disabledOptions(function () { - return Category::where('is_disabled', true) - ->get() - ->pluck('id') - ->toArray(); -}) - -``` Hide specific options in the tree ```PHP ->hiddenOptions([2, 3, 4]) ``` -```PHP -->hiddenOptions(function () { - return Category::where('is_hidden', true) - ->get() - ->pluck('id') - ->toArray(); -}) +## Filters +Use the tree in your table filters. Here's an example to show you how. + +```bash +use Filament\Tables\Filters\Filter; +use Illuminate\Database\Eloquent\Builder; +use CodeWithDennis\FilamentSelectTree\SelectTree; ``` +```php +->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 - -light -dark +![download.png](./resources/images/example.png) ## Changelog Please see [CHANGELOG](CHANGELOG.md) for more information on what has changed recently. diff --git a/resources/images/example.png b/resources/images/example.png new file mode 100644 index 0000000..51ac088 Binary files /dev/null and b/resources/images/example.png differ