resource

Compare original and translation side by side

🇺🇸

Original

English
🇨🇳

Translation

Chinese

FilamentPHP Resource Generation Skill

FilamentPHP 资源生成技能

Overview

概述

This skill generates complete FilamentPHP v4 resources including form schemas, table configurations, relation managers, and custom pages. All generated code follows official documentation patterns.
本技能可生成完整的FilamentPHP v4资源,包括表单Schema、表格配置、关联管理器和自定义页面。所有生成的代码均遵循官方文档规范。

Documentation Reference

文档参考

CRITICAL: Before generating any resource, read:
  • /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/
重要提示: 在生成任何资源之前,请阅读:
  • /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/

Workflow

工作流程

Step 1: Gather Requirements

步骤1:收集需求

Identify:
  • Model name and namespace
  • Fields to include in form
  • Columns to display in table
  • Relationships to manage
  • Custom actions needed
  • Authorization requirements
确定以下内容:
  • 模型名称和命名空间
  • 表单中需包含的字段
  • 表格中需显示的列
  • 需要管理的关联关系
  • 所需的自定义操作
  • 权限要求

Step 2: Generate Base Resource

步骤2:生成基础资源

Use Laravel artisan to create the resource:
bash
undefined
使用Laravel artisan命令创建资源:
bash
undefined

Basic resource

基础资源

php artisan make:filament-resource ModelName
php artisan make:filament-resource ModelName

With generate flag (creates form/table from model)

使用--generate标志(从模型生成表单/表格)

php artisan make:filament-resource ModelName --generate
php artisan make:filament-resource ModelName --generate

Soft deletes support

支持软删除

php artisan make:filament-resource ModelName --soft-deletes
php artisan make:filament-resource ModelName --soft-deletes

View page only

仅查看页面

php artisan make:filament-resource ModelName --view
php artisan make:filament-resource ModelName --view

Simple resource (modal forms instead of pages)

简易资源(使用模态框表单而非独立页面)

php artisan make:filament-resource ModelName --simple
undefined
php artisan make:filament-resource ModelName --simple
undefined

Step 3: Customize Form Schema

步骤3:自定义表单Schema

Read form field documentation and implement:
php
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',
                        ]),
                ]),
        ]);
}
阅读表单字段文档并实现:
php
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',
                        ]),
                ]),
        ]);
}

Step 4: Customize Table

步骤4:自定义表格

Read table documentation and implement:
php
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(),
            ]),
        ]);
}
阅读表格文档并实现:
php
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(),
            ]),
        ]);
}

Step 5: Add Relation Managers

步骤5:添加关联管理器

For relationships, create relation managers:
bash
php artisan make:filament-relation-manager ResourceName RelationName column_name
Register in resource:
php
public static function getRelations(): array
{
    return [
        RelationManagers\CommentsRelationManager::class,
        RelationManagers\TagsRelationManager::class,
    ];
}
对于关联关系,创建关联管理器:
bash
php artisan make:filament-relation-manager ResourceName RelationName column_name
在资源中注册:
php
public static function getRelations(): array
{
    return [
        RelationManagers\CommentsRelationManager::class,
        RelationManagers\TagsRelationManager::class,
    ];
}

Step 6: Configure Pages

步骤6:配置页面

Define resource pages:
php
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'),
    ];
}
定义资源页面:
php
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'),
    ];
}

Step 7: Add Authorization

步骤7:添加权限控制

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

public static function canCreate(): bool
{
    return auth()->user()->can('create_model');
}
实现策略方法:
php
public static function canViewAny(): bool
{
    return auth()->user()->can('view_any_model');
}

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

Form Field Reference

表单字段参考

Text Fields

文本字段

  • TextInput::make()
    - Single line text
  • Textarea::make()
    - Multi-line text
  • RichEditor::make()
    - WYSIWYG editor
  • MarkdownEditor::make()
    - Markdown editor
  • TextInput::make()
    - 单行文本
  • Textarea::make()
    - 多行文本
  • RichEditor::make()
    - 所见即所得编辑器
  • MarkdownEditor::make()
    - Markdown编辑器

Selection Fields

选择字段

  • Select::make()
    - Dropdown select
  • Radio::make()
    - Radio buttons
  • Checkbox::make()
    - Single checkbox
  • CheckboxList::make()
    - Multiple checkboxes
  • Toggle::make()
    - Toggle switch
  • Select::make()
    - 下拉选择框
  • Radio::make()
    - 单选按钮
  • Checkbox::make()
    - 单个复选框
  • CheckboxList::make()
    - 多个复选框
  • Toggle::make()
    - 开关按钮

Date/Time Fields

日期/时间字段

  • DatePicker::make()
    - Date only
  • DateTimePicker::make()
    - Date and time
  • TimePicker::make()
    - Time only
  • DatePicker::make()
    - 仅日期
  • DateTimePicker::make()
    - 日期和时间
  • TimePicker::make()
    - 仅时间

File Fields

文件字段

  • FileUpload::make()
    - File upload
  • SpatieMediaLibraryFileUpload::make()
    - Media library
  • FileUpload::make()
    - 文件上传
  • SpatieMediaLibraryFileUpload::make()
    - 媒体库

Relationship Fields

关联字段

  • Select::make()->relationship()
    - BelongsTo select
  • CheckboxList::make()->relationship()
    - BelongsToMany
  • Repeater::make()->relationship()
    - HasMany inline
  • Select::make()->relationship()
    - BelongsTo选择框
  • CheckboxList::make()->relationship()
    - BelongsToMany关联
  • Repeater::make()->relationship()
    - HasMany内联关联

Layout Components

布局组件

  • Section::make()
    - Card section
  • Fieldset::make()
    - Fieldset grouping
  • Tabs::make()
    - Tabbed sections
  • Grid::make()
    - Grid layout
  • Split::make()
    - Split layout
  • Section::make()
    - 卡片区块
  • Fieldset::make()
    - 字段集分组
  • Tabs::make()
    - 标签页区块
  • Grid::make()
    - 网格布局
  • Split::make()
    - 拆分布局

Table Column Reference

表格列参考

Text Columns

文本列

  • TextColumn::make()
    - Basic text
  • IconColumn::make()
    - Boolean icon
  • ImageColumn::make()
    - Image thumbnail
  • BadgeColumn::make()
    - Badge styling
  • ColorColumn::make()
    - Color swatch
  • TextColumn::make()
    - 基础文本
  • IconColumn::make()
    - 布尔值图标
  • ImageColumn::make()
    - 图片缩略图
  • BadgeColumn::make()
    - 徽章样式
  • ColorColumn::make()
    - 颜色样本

Column Modifiers

列修饰符

  • ->searchable()
    - Enable search
  • ->sortable()
    - Enable sort
  • ->toggleable()
    - Can hide/show
  • ->wrap()
    - Wrap text
  • ->limit()
    - Truncate text
  • ->searchable()
    - 启用搜索
  • ->sortable()
    - 启用排序
  • ->toggleable()
    - 可隐藏/显示
  • ->wrap()
    - 文本换行
  • ->limit()
    - 文本截断

Output

输出结果

For each resource, generate:
  1. Resource class -
    app/Filament/Resources/ModelResource.php
  2. Pages -
    app/Filament/Resources/ModelResource/Pages/
  3. Relation Managers -
    app/Filament/Resources/ModelResource/RelationManagers/
  4. Test file -
    tests/Feature/Filament/ModelResourceTest.php
对于每个资源,将生成以下内容:
  1. 资源类 -
    app/Filament/Resources/ModelResource.php
  2. 页面 -
    app/Filament/Resources/ModelResource/Pages/
  3. 关联管理器 -
    app/Filament/Resources/ModelResource/RelationManagers/
  4. 测试文件 -
    tests/Feature/Filament/ModelResourceTest.php