mcpField
R
resource

resource

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

关于 resource

resource 是面向 FilamentPHP v4 开发者的资源生成技能,可生成资源类、表单、表格、关系管理器和页面。它按官方文档模式组织代码,并包含授权配置与测试文件。

主要功能

  1. 01生成 FilamentPHP v4 资源
  2. 02配置表单字段与布局
  3. 03配置表格列及操作
  4. 04创建关系管理器和页面
  5. 05添加授权逻辑与测试文件

适用场景

  1. 01为 Laravel 模型创建管理资源
  2. 02为资源配置关联数据管理
  3. 03生成含授权的 Filament 管理界面

安装

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

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

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

详细介绍

FilamentPHP 资源生成技能

概述

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

文档参考

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

  • /home/mwguerra/projects/mwguerra/claude-code-plugins/filament-specialist/skills/docs/references/general/03-resources/
  • /home/mwguerra/projects/mwguerra/claude-code-plugins/filament-specialist/skills/docs/references/forms/
  • /home/mwguerra/projects/mwguerra/claude-code-plugins/filament-specialist/skills/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