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('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));

->placeholder(__('Please select a category'))

->enableBranchNode()

->emptyLabel(__('Oops, no results have been found!'))

->withCount()

->alwaysOpen()

->independent(false)

->expandSelected(false)

->parentNullValue(-1)

->defaultOpenLevel(2)

->direction('top')

->grouped(false)

->clearable(false)

->searchable();

->disabledOptions([2, 3, 4])

->hiddenOptions([2, 3, 4])

->withTrashed()

->withKey('code')

->storeResults()

->disabledOptions(function ($state, SelectTree $component) {
    $results = $component->getResults();
})

->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