resource
Compare original and translation side by side
🇺🇸
Original
English🇨🇳
Translation
ChineseFilamentPHP 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
undefinedBasic 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
undefinedphp artisan make:filament-resource ModelName --simple
undefinedStep 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_nameRegister 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
文本字段
- - Single line text
TextInput::make() - - Multi-line text
Textarea::make() - - WYSIWYG editor
RichEditor::make() - - Markdown editor
MarkdownEditor::make()
- - 单行文本
TextInput::make() - - 多行文本
Textarea::make() - - 所见即所得编辑器
RichEditor::make() - - Markdown编辑器
MarkdownEditor::make()
Selection Fields
选择字段
- - Dropdown select
Select::make() - - Radio buttons
Radio::make() - - Single checkbox
Checkbox::make() - - Multiple checkboxes
CheckboxList::make() - - Toggle switch
Toggle::make()
- - 下拉选择框
Select::make() - - 单选按钮
Radio::make() - - 单个复选框
Checkbox::make() - - 多个复选框
CheckboxList::make() - - 开关按钮
Toggle::make()
Date/Time Fields
日期/时间字段
- - Date only
DatePicker::make() - - Date and time
DateTimePicker::make() - - Time only
TimePicker::make()
- - 仅日期
DatePicker::make() - - 日期和时间
DateTimePicker::make() - - 仅时间
TimePicker::make()
File Fields
文件字段
- - File upload
FileUpload::make() - - Media library
SpatieMediaLibraryFileUpload::make()
- - 文件上传
FileUpload::make() - - 媒体库
SpatieMediaLibraryFileUpload::make()
Relationship Fields
关联字段
- - BelongsTo select
Select::make()->relationship() - - BelongsToMany
CheckboxList::make()->relationship() - - HasMany inline
Repeater::make()->relationship()
- - BelongsTo选择框
Select::make()->relationship() - - BelongsToMany关联
CheckboxList::make()->relationship() - - HasMany内联关联
Repeater::make()->relationship()
Layout Components
布局组件
- - Card section
Section::make() - - Fieldset grouping
Fieldset::make() - - Tabbed sections
Tabs::make() - - Grid layout
Grid::make() - - Split layout
Split::make()
- - 卡片区块
Section::make() - - 字段集分组
Fieldset::make() - - 标签页区块
Tabs::make() - - 网格布局
Grid::make() - - 拆分布局
Split::make()
Table Column Reference
表格列参考
Text Columns
文本列
- - Basic text
TextColumn::make() - - Boolean icon
IconColumn::make() - - Image thumbnail
ImageColumn::make() - - Badge styling
BadgeColumn::make() - - Color swatch
ColorColumn::make()
- - 基础文本
TextColumn::make() - - 布尔值图标
IconColumn::make() - - 图片缩略图
ImageColumn::make() - - 徽章样式
BadgeColumn::make() - - 颜色样本
ColorColumn::make()
Column Modifiers
列修饰符
- - Enable search
->searchable() - - Enable sort
->sortable() - - Can hide/show
->toggleable() - - Wrap text
->wrap() - - Truncate text
->limit()
- - 启用搜索
->searchable() - - 启用排序
->sortable() - - 可隐藏/显示
->toggleable() - - 文本换行
->wrap() - - 文本截断
->limit()
Output
输出结果
For each resource, generate:
- Resource class -
app/Filament/Resources/ModelResource.php - Pages -
app/Filament/Resources/ModelResource/Pages/ - Relation Managers -
app/Filament/Resources/ModelResource/RelationManagers/ - Test file -
tests/Feature/Filament/ModelResourceTest.php
对于每个资源,将生成以下内容:
- 资源类 -
app/Filament/Resources/ModelResource.php - 页面 -
app/Filament/Resources/ModelResource/Pages/ - 关联管理器 -
app/Filament/Resources/ModelResource/RelationManagers/ - 测试文件 -
tests/Feature/Filament/ModelResourceTest.php