mcpField
F
filament-resource

filament-resource

github.com/mwguerra开发361.2k 次安装26 周前更新
代码仓库

关于 filament-resource

FilamentPHP Resource Generation Skill 指导开发者生成 FilamentPHP v4 资源,涵盖表单、表格、关系管理器和页面。工作流还包括授权配置与功能测试文件,并参照指定文档模式。

主要功能

  1. 01生成 FilamentPHP v4 资源
  2. 02配置表单和表格
  3. 03创建关系管理器与页面
  4. 04添加授权策略
  5. 05生成特性测试文件

适用场景

  1. 01为模型搭建管理资源
  2. 02在资源中配置关联记录
  3. 03添加授权与资源测试

安装

复制下面的命令,在终端中运行。

$npx skills add https://github.com/mwguerra/claude-code-plugins --skill filament-resource

安装后在 Claude Code 中输入/filament-resource确认已加载。

详细介绍

FilamentPHP 资源生成技能

概述

此技能可生成完整的 FilamentPHP v4 资源,包括表单架构、表格配置、关系管理器和自定义页面。所有生成的代码均遵循官方文档中的模式。

文档参考

重要: 生成任何资源之前,请阅读:

  • /home/mwguerra/projects/mwguerra/claude-code-plugins/filament-specialist/skills/filament-docs/references/general/03-resources/
  • /home/mwguerra/projects/mwguerra/claude-code-plugins/filament-specialist/skills/filament-docs/references/forms/
  • /home/mwguerra/projects/mwguerra/claude-code-plugins/filament-specialist/skills/filament-docs/references/tables/

工作流

步骤 1:收集需求

确定:

  • 模型名称和命名空间
  • 表单中要包含的字段
  • 表格中要显示的列
  • 要管理的关系
  • 所需的自定义操作
  • 授权要求
步骤 2:生成基础资源

使用 Laravel artisan 创建资源:

# Basic resource
php artisan make:filament-resource ModelName

# With generate flag (creates form/table from model)
php artisan make:filament-resource ModelName --generate

# Soft deletes support
php artisan make:filament-resource ModelName --soft-deletes

# View page only
php artisan make:filament-resource ModelName --view

# Simple resource (modal forms instead of pages)
php artisan make:filament-resource ModelName --simple
步骤 3:自定义表单架构

阅读表单字段文档并实现:

use Filament\Forms;
use Filament\Forms\Form;

public static function form(Form $form): Form
{
    return $form
        ->schema([
            Forms\Components\Section::make('Basic Information')
                ->schema([
                    Forms\Components\TextInput::make('name')
                        ->required()
                        ->maxLength(255),
                    Forms\Components\Textarea::make('description')
                        ->rows(3)
                        ->columnSpanFull(),
                ]),
            Forms\Components\Section::make('Settings')
                ->schema([
                    Forms\Components\Toggle::make('is_active')
                        ->default(true),
                    Forms\Components\Select::make('status')
                        ->options([
                            'draft' => 'Draft',
                            'published' => 'Published',
                        ]),
                ]),
        ]);
}
步骤 4:自定义表格

阅读表格文档并实现:

use Filament\Tables;
use Filament\Tables\Table;

public static function table(Table $table): Table
{
    return $table
        ->columns([
            Tables\Columns\TextColumn::make('name')
                ->searchable()
                ->sortable(),
            Tables\Columns\IconColumn::make('is_active')
                ->boolean(),
            Tables\Columns\BadgeColumn::make('status')
                ->colors([
                    'warning' => 'draft',
                    'success' => 'published',
                ]),
            Tables\Columns\TextColumn::make('created_at')
                ->dateTime()
                ->sortable()
                ->toggleable(isToggledHiddenByDefault: true),
        ])
        ->filters([
            Tables\Filters\SelectFilter::make('status')
                ->options([
                    'draft' => 'Draft',
                    'published' => 'Published',
                ]),
            Tables\Filters\TernaryFilter::make('is_active'),
        ])
        ->actions([
            Tables\Actions\ViewAction::make(),
            Tables\Actions\EditAction::make(),
            Tables\Actions\DeleteAction::make(),
        ])
        ->bulkActions([
            Tables\Actions\BulkActionGroup::make([
                Tables\Actions\DeleteBulkAction::make(),
            ]),
        ]);
}
步骤 5:添加关系管理器

对于关系,创建关系管理器:

php artisan make:filament-relation-manager ResourceName RelationName column_name

在资源中注册:

public static function getRelations(): array
{
    return [
        RelationManagers\CommentsRelationManager::class,
        RelationManagers\TagsRelationManager::class,
    ];
}
步骤 6:配置页面

定义资源页面:

public static function getPages(): array
{
    return [
        'index' => Pages\ListModels::route('/'),
        'create' => Pages\CreateModel::route('/create'),
        'view' => Pages\ViewModel::route('/{record}'),
        'edit' => Pages\EditModel::route('/{record}/edit'),
    ];
}
步骤 7:添加授权

实现策略方法:

public static function canViewAny(): bool
{
    return auth()->user()->can('view_any_model');
}

public static function canCreate(): bool
{
    return auth()->user()->can('create_model');
}

表单字段参考

文本字段
  • TextInput::make() - 单行文本
  • Textarea::make() - 多行文本
  • RichEditor::make() - 所见即所得编辑器
  • MarkdownEditor::make() - Markdown 编辑器
选择字段
  • Select::make() - 下拉选择
  • Radio::make() - 单选按钮
  • Checkbox::make() - 单个复选框
  • CheckboxList::make() - 多个复选框
  • Toggle::make() - 切换开关
日期/时间字段
  • DatePicker::make() - 仅日期
  • DateTimePicker::make() - 日期和时间
  • TimePicker::make() - 仅时间
文件字段
  • FileUpload::make() - 文件上传
  • SpatieMediaLibraryFileUpload::make() - 媒体库
关系字段
  • Select::make()->relationship() - BelongsTo 选择
  • CheckboxList::make()->relationship() - BelongsToMany
  • Repeater::make()->relationship() - 内联 HasMany
布局组件
  • Section::make() - 卡片分区
  • Fieldset::make() - 字段集分组
  • Tabs::make() - 选项卡分区
  • Grid::make() - 网格布局
  • Split::make() - 分栏布局

表格列参考

文本列
  • TextColumn::make() - 基本文本
  • IconColumn::make() - 布尔图标
  • ImageColumn::make() - 图片缩略图
  • BadgeColumn::make() - 徽章样式
  • ColorColumn::make() - 颜色样本
列修饰符
  • ->searchable() - 启用搜索
  • ->sortable() - 启用排序
  • ->toggleable() - 可隐藏/显示
  • ->wrap() - 自动换行
  • ->limit() - 截断文本

输出

为每个资源生成:

  1. 资源类 - app/Filament/Resources/ModelResource.php
  2. 页面 - app/Filament/Resources/ModelResource/Pages/
  3. 关系管理器 - app/Filament/Resources/ModelResource/RelationManagers/
  4. 测试文件 - tests/Feature/Filament/ModelResourceTest.php

评论

支持 Markdown