PHP code example of codewithdennis / filament-select-tree
1. Go to this page and download the library: Download codewithdennis/filament-select-tree library . Choose the download type require .
2. Extract the ZIP file and open the index.php.
3. Add this code to the index.php.
<?php
require_once('vendor/autoload.php');
/* Start to develop here. Best regards https://php-download.com/ */
codewithdennis / filament-select-tree example snippets
SelectTree::make('categories')
->relationship('categories', 'name', 'parent_id')
SelectTree::make('category_id')
->relationship('category', 'name', 'parent_id')
SelectTree::make('category_id')
->query(fn() => Category::query(), 'name', 'parent_id')
SelectTree::make('categories')
->relationship(relationship: 'categories', titleAttribute: 'name', parentAttribute: 'parent_id', modifyQueryUsing: fn($query) => $query));
SelectTree::make('categories')
->relationship(relationship: 'categories', titleAttribute: 'name', parentAttribute: 'parent_id', modifyChildQueryUsing: fn($query) => $query));
SelectTree::make('categories')
->relationship(relationship: 'categories', titleAttribute: 'name', parentAttribute: 'parent_id', modifyQueryUsing: fn($query) => $query->where('id', 1))
->strictNullParentRootNodes(); // only children of parent results from the parent query will be
->placeholder(__('Please select a category'))
->enableBranchNode()
->emptyLabel(__('Oops, no results have been found!'))
->withCount()
->alwaysOpen()
->staticList()
->independent(false)
->expandSelected(false)
->parentNullValue(-1)
->defaultOpenLevel(2)
->direction('top')
->grouped(false)
->isGroupedValue()
->showTags(false)
->tagsCountText('elements selected')
->clearable(false)
->searchable();
->disabledOptions([2, 3, 4])
->hiddenOptions([2, 3, 4])
->withTrashed()
->withKey('code')
->storeResults()
->disabledOptions(function ($state, SelectTree $component) {
$results = $component->getResults();
})
->multiple(false)
->treeKey('my-cool-tree')
use Filament\Tables\Filters\Filter;
use Illuminate\Database\Eloquent\Builder;
use CodeWithDennis\FilamentSelectTree\SelectTree;
->filters([
Filter::make('tree')
->form([
SelectTree::make('category')
->relationship('categories', 'name', 'parent_id')
->enableBranchNode()
->multiple(false)
->prepend([
'name' => 'Uncategorized Records',
'value' => -1,
'parent' => null, // optional
'disabled' => false, // optional
'hidden' => false, // optional
'children' => [], // optional
])
])
->query(function (Builder $query, array $data) {
return $query->when($data['categories'], function (Builder $query, $categories) {
if (collect($categories)->contains('-1')) {
$query->whereDoesntHave('categories');
}
return $query->orWhereHas('categories',
fn(Builder $query) => $query->whereIn('id', $categories));
});
})
])
->schema([
SelectTree::make('category')
->relationship('categories', 'name', 'parent_id')
->enableBranchNode()
->multiple(false)
->append([
'name' => 'Uncategorized Records',
'value' => -1,
'parent' => null, // optional
'disabled' => false, // optional
'hidden' => false, // optional
'children' => [], // optional
])
])
SelectTree::make('categories')
->getTreeUsing([
[
'name' => 'Parent Category',
'value' => '1',
'children' => [
[
'name' => 'Child Category',
'value' => '2',
'children' => [],
],
],
],
[
'name' => 'Another Category',
'value' => '3',
'children' => [],
],
])
SelectTree::make('categories')
->getTreeUsing(function () {
return Category::query()
->get()
->map(fn ($category) => [
'name' => $category->name,
'value' => $category->id,
'children' => $category->children->map(fn ($child) => [
'name' => $child->name,
'value' => $child->id,
'children' => [],
])->toArray(),
])
->toArray();
})
[
[
'name' => 'Display Name',
'value' => 'option_value',
'disabled' => false, // optional
'hidden' => false, // optional
'children' => [], // optional
],
]
->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());
})
])
bash
php artisan filament:assets