PHP code example of saade / filament-adjacency-list

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

    

saade / filament-adjacency-list example snippets


use Filament\Forms\Components\TextInput;
use Saade\FilamentAdjacencyList\Forms\Components\AdjacencyList;

AdjacencyList::make('menu')
    ->schema([
        TextInput::make('label')
            ->

class Menu extends Model
{
    protected function casts(): array
    {
        return [
            'items' => 'array',
        ];
    }
}

AdjacencyList::make('items')
    ->schema([
        TextInput::make('label')->

[
    '9c0b1c4e-...' => [
        'label' => 'Products',
        'url' => '/products',
        'children' => [
            '4f7d2a90-...' => [
                'label' => 'Books',
                'url' => '/products/books',
                'children' => [],
            ],
        ],
    ],
]

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

use Illuminate\Database\Eloquent\Model;
use Staudenmeir\LaravelAdjacencyList\Eloquent\HasRecursiveRelationships;

class Category extends Model
{
    use HasRecursiveRelationships;

    protected $fillable = ['parent_id', 'name', 'sort'];
}

// app/Filament/Resources/Categories/Schemas/CategoryForm.php

AdjacencyList::make('descendants')
    ->relationship('descendants')
    ->labelKey('name')
    ->orderColumn('sort')
    ->schema([
        TextInput::make('name')
            ->

AdjacencyList::make('descendants')
    ->relationship('descendants')
    ->saveOnReorder()

AdjacencyList::make('descendants')
    ->relationship('descendants', fn (Builder $query): Builder => $query->where('is_enabled', true))

AdjacencyList::make('descendants')
    ->relationship('descendants')
    ->orderColumn('position')

AdjacencyList::make('descendants')
    ->relationship('descendants')
    ->mutateRelationshipDataBeforeFillUsing(fn (array $data): array => $data)
    ->mutateRelationshipDataBeforeCreateUsing(fn (array $data): array => [...$data, 'created_by' => auth()->id()])
    ->mutateRelationshipDataBeforeSaveUsing(fn (array $data, Model $record): array => $data)

use Illuminate\Database\Eloquent\Model;
use Staudenmeir\LaravelAdjacencyList\Eloquent\HasGraphRelationships;

class Node extends Model
{
    use HasGraphRelationships;

    public function getPivotTableName(): string
    {
        return 'edges';
    }
}

AdjacencyList::make('descendants')
    ->relationship('descendants')
    ->labelKey('name')

AdjacencyList::make('items')
    ->labelKey('name') // defaults to 'label'

AdjacencyList::make('items')
    ->itemLabel(fn (array $item): string => "{$item['name']} ({$item['code']})")

AdjacencyList::make('items')
    ->childrenKey('items')

AdjacencyList::make('items')
    ->maxDepth(2)

AdjacencyList::make('items')
    ->itemAction('edit')

AdjacencyList::make('descendants')
    ->itemUrl(fn (array $item): string => CategoryResource::getUrl('edit', ['record' => $item['id']]))
    ->openItemUrlInNewTab()

AdjacencyList::make('items')
    ->collapsible()

AdjacencyList::make('items')
    ->collapsed() // collapsible, and collapsed when the form loads

AdjacencyList::make('items')
    ->collapsible()
    ->persistCollapsed()

AdjacencyList::make('items')
    ->rulers()

AdjacencyList::make('items')
    ->modal(false)

AdjacencyList::make('items')
    ->addable(false)
    ->editable(false)
    ->deletable(false)
    ->reorderable(false) // dragging
    ->moveable(false)    // the move up and move down buttons
    ->indentable(false)  // the indent and outdent buttons

use Filament\Actions\Action;

AdjacencyList::make('items')
    ->addAction(fn (Action $action): Action => $action->label('Add link'))
    ->addChildAction(fn (Action $action): Action => $action->icon('heroicon-o-plus'))
    ->editAction(fn (Action $action): Action => $action->slideOver())
    ->deleteAction(fn (Action $action): Action => $action->): Action => $action->label('Down'))

use Filament\Forms\Components\TextInput;
use Saade\FilamentAdjacencyList\Forms\Components\AdjacencyList;
use Saade\FilamentAdjacencyList\Widgets\AdjacencyListWidget;

class CategoryTreeWidget extends AdjacencyListWidget
{
    protected function adjacencyList(AdjacencyList $adjacencyList): AdjacencyList
    {
        return $adjacencyList
            ->labelKey('name')
            ->orderColumn('sort')
            ->schema([
                TextInput::make('name')->

protected function getModel(): ?Model
{
    return Category::query()->whereNull('parent_id')->first();
}
css
@source '../../../../vendor/saade/filament-adjacency-list/resources/views/**/*.blade.php';