1. Go to this page and download the library: Download beholdr/filament-trilist 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/ */
use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\Schema;
return new class extends Migration
{
public function up(): void
{
Schema::create('professions', function (Blueprint $table) {
$table->id();
$table->string('label');
});
Schema::create('profession_profession', function (Blueprint $table) {
$table->primary(['parent_id', 'child_id']);
$table->foreignId('parent_id')->constrained('professions')->cascadeOnDelete();
$table->foreignId('child_id')->constrained('professions')->cascadeOnDelete();
});
}
public function down(): void
{
Schema::dropIfExists('professions');
Schema::dropIfExists('profession_profession');
}
};
namespace App\Models;
use Illuminate\Contracts\Database\Eloquent\Builder;
use Illuminate\Database\Eloquent\Model;
class Profession extends Model
{
protected $with = ['children'];
public function parents()
{
return $this->belongsToMany(Profession::class, 'profession_profession', 'child_id', 'parent_id');
}
public function children()
{
return $this->belongsToMany(Profession::class, 'profession_profession', 'parent_id', 'child_id');
}
public function scopeRoot(Builder $builder)
{
$builder->doesntHave('parents');
}
}
Profession::root()->get();
use Beholdr\FilamentTrilist\Components\TrilistSelect
// with custom tree data
TrilistSelect::make('category_id')
->options($treeData),
// or with relationship
TrilistSelect::make('categories')
->relationship('categories')
->options($treeData)
->multiple(),
TrilistSelect::make(string $fieldName)
->label(string $fieldLabel)
->placeholder(string | Closure $placeholder)
->disabled(bool | Closure $condition)
// array of tree items
->options(array | Closure $options),
// first argument defines name of the relationship, second can be used to modify relationship query
->relationship(string | Closure $relationshipName, ?Closure $modifyQueryUsing = null)
// array of ids (or single id) of disabled items
->disabledOptions(string | int | array | Closure $value)
// multiple selection mode, default: false
->multiple(bool | Closure $condition)
// animate expand/collapse, default: true
->animated(bool | Closure $condition)
// expand initial selected options, default: true
->expandSelected(bool | Closure $condition)
// in independent mode children auto selected when parent is selected, default: false
->independent(bool | Closure $condition)
// in leafs mode, the selected value is not grouped as the parent when all child elements are selected, default: false
->leafs(bool | Closure $condition)
// tree item id field name, default: 'id'
->fieldId(string | Closure $value)
// tree item label field name, default: 'label'
->fieldLabel(string | Closure $value)
// tree item children field name, default: 'children'
->fieldChildren(string | Closure $value)
// hook for generating custom labels, default: '(item) => item.label'
->labelHook(string | Closure $value)
// enable filtering of items, default: false
->searchable(bool | Closure $condition)
// enable autofocus on filter field, default: false
->autofocus(bool | Closure $condition)
// search input placeholder
->searchPrompt(string | Htmlable | Closure $message)
// select button label
->selectButton(string | Htmlable | Closure $message)
// cancel button label
->cancelButton(string | Htmlable | Closure $message)
namespace App\Filament\Resources\PostResource\Pages;
use App\Filament\Resources\PostResource;
use App\Models\Post;
use Beholdr\FilamentTrilist\Components\TrilistPage;
class TreePosts extends TrilistPage
{
protected static string $resource = PostResource::class;
// optional page and tab title
protected static ?string $title = 'Posts Tree';
// return array of tree items (see below about tree data)
public function getTreeOptions(): array
{
return Post::root()->get()->toArray();
}
}
public static function getPages(): array
{
return [
// ...
'tree' => Pages\TreePosts::route('/tree'),
];
}
use App\Filament\Resources\PostResource\Pages\TreePosts;
use Beholdr\FilamentTrilist\FilamentTrilistPlugin;
use Filament\Navigation\NavigationItem;
class AdminPanelProvider extends PanelProvider
{
public function panel(Panel $panel): Panel
{
return $panel
->navigationItems(TreePosts::getNavigationItems())
}
}
class TreeCategories extends TrilistPage
{
public static function getFieldLabel(): string
{
return 'name';
}
}