PHP code example of alareqi / filament-tree

1. Go to this page and download the library: Download alareqi/filament-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/ */

    

alareqi / filament-tree example snippets


Schema::create('categories', function (Blueprint $table): void {
    $table->id();
    $table->string('name');
    $table->foreignId('parent_id')->nullable()->constrained('categories')->nullOnDelete();
    $table->unsignedInteger('sort_order')->default(0);
    $table->timestamps();
});

use Alareqi\FilamentTree\Concerns\InteractsWithTreeTable;
use Filament\Resources\Pages\ListRecords;

class ListCategories extends ListRecords
{
    use InteractsWithTreeTable;
}

use Alareqi\FilamentTree\Columns\TreeColumn;
use Filament\Tables\Columns\TextColumn;
use Filament\Tables\Table;

public static function table(Table $table): Table
{
    return $table
        ->columns([
            TreeColumn::make('name')->searchable(),
            TextColumn::make('updated_at')->dateTime()->sortable(),
        ])
        ->reorderable('sort_order')
        ->tree(
            parentColumn: 'parent_id',
            treeColumn: 'name',
        );
}

use Alareqi\FilamentTree\Forms\Components\TreeSelect;

TreeSelect::make('parent_id')
    ->label('Parent category')
    ->treeOptions(fn () => Category::query()
        ->orderBy('sort_order')
        ->get()
        ->map(fn (Category $category): array => [
            'value' => $category->getKey(),
            'parent' => $category->parent_id,
            'label' => $category->name,
        ]))
    ->placeholder('No parent')
    ->searchable();