saade/filament-adjacency-list

一个用于邻接表的 Filament 包。

v4.0.0-beta1 2024-08-26 17:00 UTC

This package is auto-updated.

Last update: 2024-08-26 17:23:17 UTC


README

Latest Version on Packagist Total Downloads

用于管理邻接表(即树)的 Filament 包。

Banner

安装

您可以通过 composer 安装此包

composer require saade/filament-adjacency-list

使用方法

use Saade\FilamentAdjacencyList\Forms\Components\AdjacencyList;

AdjacencyList::make('subjects')
    ->form([
        Forms\Components\TextInput::make('label')
            ->required(),
    ])

配置

自定义用于显示项目标签的 label

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

自定义用于收集项目子项的 children 键。

注意: 这仅在未使用关系时使用。

AdjacencyList::make('subjects')
    ->childrenKey('children')   // defaults to 'children'

自定义树的 MaxDepth

AdjacencyList::make('subjects')
    ->maxDepth(2)               // defaults to -1 (unlimited depth)

不使用模态创建项目。

AdjacencyList::make('subjects')
    ->modal(false)      // defaults to true

禁用创建、编辑、删除和重新排序。

AdjacencyList::make('subjects')
    ->addable(false)
    ->editable(false)
    ->deletable(false)
    ->reorderable(false)

自定义操作

use Filament\Forms\Actions\Action;

AdjacencyList::make('subjects')
    ->addAction(fn (Action $action): Action => $action->icon('heroicon-o-plus')->color('primary'))
    ->addChildAction(fn (Action $action): Action => $action->button())
    ->editAction(fn (Action $action): Action => $action->icon('heroicon-o-pencil'))
    ->deleteAction(fn (Action $action): Action => $action->requiresConfirmation())
    ->reorderAction(fn (Action $action): Action => $action->icon('heroicon-o-arrow-path-rounded-square'))

重要

重新排序操作

如果您想向操作添加 ->extraAttributes(),需要将数组中的 'data-sortable-handle' => 'true' 添加到数组中,因为操作作为 SortableJS 的处理程序。

默认情况下,点击操作将执行任何操作。如果您想在点击时触发某些操作,需要在操作上链式调用 ->livewireClickHandlerEnabled()

关系

在这个例子中,我们将创建一个票务系统,其中票据可以被分配给一个部门,而部门有主题。

建立关系

// App/Models/Department.php

class Department extends Model
{
    public function subjects(): HasMany
    {
        return $this->hasMany(Subject::class)->whereNull('parent_id')->with('children')->orderBy('sort');
    }
}
// App/Models/Subject.php

class Subject extends Model
{
    protected $fillable ['parent_id', 'name', 'sort']; // or whatever your columns are

    public function children(): HasMany
    {
        return $this->hasMany(Subject::class, 'parent_id')->with('children')->orderBy('sort');
    }
}

现在您已经创建了部门和主题之间的嵌套关系。

使用关系

// App/Filament/Resources/DepartmentResource.php

AdjacencyList::make('subjects')
    ->relationship('subjects')          // Define the relationship
    ->labelKey('name')                  // Customize the label key to your model's column
    ->childrenKey('children')           // Customize the children key to the relationship's method name
    ->form([                            // Define the form
        Forms\Components\TextInput::make('name')
            ->label(__('Name'))
            ->required(),
    ]);

这就完成了!现在您可以使用关系来管理您的邻接表。

与 Staudenmeir 的 Laravel 邻接表一起工作

此包还支持 Staudenmeir 的 Laravel 邻接表 包。

首先,安装包

composer require staudenmeir/laravel-adjacency-list:"^1.0"
  1. 在您的模型中使用 HasRecursiveRelationships 特性,并重写默认路径分隔符。
// App/Models/Department.php

class Department extends Model
{
    use \Staudenmeir\LaravelAdjacencyList\Eloquent\HasRecursiveRelationships;

    public function getPathSeparator()
    {
        return '.children.';
    }
}

如果您已经在应用程序的其他部分使用了 HasRecursiveRelationships 特性,那么改变模型路径分隔符可能不是个好主意,因为它可能会破坏应用程序的其他部分。相反,您可以根据需要添加尽可能多的路径分隔符

class Department extends Model
{
    use \Staudenmeir\LaravelAdjacencyList\Eloquent\HasRecursiveRelationships;

    public function getCustomPaths()
    {
        return [
            [
                'name' => 'tree_path',
                'column' => 'id',
                'separator' => '.children.',
            ],
        ];
    }
}
  1. 使用 relationship 方法来定义关系
AdjacencyList::make('subdepartments')
    ->relationship('descendants')   // or 'descendantsAndSelf', 'children' ...
    ->customPath('tree_path')       // if you're using custom paths

这就完成了!现在您可以使用关系来管理您的邻接表。

自定义查询

AdjacencyList::make('subdepartments')
    ->relationship('descendants', fn (Builder $query): Builder => $query->where('enabled', 1))

排序

如果您的应用程序需要按顺序排列列表中的项,您可以使用 orderColumn 方法

AdjacencyList::make('subdepartments')
    ->orderColumn('sort')   // or any other column

变更日志

请参阅 CHANGELOG 了解最近更改的详细信息。

贡献

请参阅 CONTRIBUTING 了解详细信息。

安全漏洞

请查看 我们的安全策略 了解如何报告安全漏洞。

鸣谢

许可

MIT 许可证(MIT)。请参阅 许可文件 了解更多详细信息。

Sponsor Saade