公告: 旧版文档请移步 -> http://v2.laravel-filament.cn/docs

Languages

Version

Theme

Actions - 预制 Action

Create action

概述

Filament 包含一个可以创建 Eloquent 记录的预制 Action。点击触发按钮后,会打开一个带有表单的模态框。用户填写表单,数据经验证后存入到数据库。你可以这样使用:

use Filament\Actions\CreateAction;
use Filament\Forms\Components\TextInput;
 
CreateAction::make()
->model(Post::class)
->form([
TextInput::make('title')
->required()
->maxLength(255),
// ...
])

如果你想将该 Action 添加到表格的头部,你可以使用 Filament\Tables\Actions\CreateAction

use Filament\Forms\Components\TextInput;
use Filament\Tables\Actions\CreateAction;
use Filament\Tables\Table;
 
public function table(Table $table): Table
{
return $table
->headerActions([
CreateAction::make()
->form([
TextInput::make('title')
->required()
->maxLength(255),
// ...
]),
]);
}

保持前自定义数据

有时候,你需要在表单数据存入到数据库前对其进行修改。为此,你可以使用 mutateFormDataUsing() 方法,该方法能够以数组的方式访问 $data 并返回修改后的版本:

CreateAction::make()
->mutateFormDataUsing(function (array $data): array {
$data['user_id'] = auth()->id();
 
return $data;
})

自定义创建过程

你可以使用 using() 方法微调记录的创建方式:

use Illuminate\Database\Eloquent\Model;
 
CreateAction::make()
->using(function (array $data, string $model): Model {
return $model::create($data);
})

$model 是模型类名,不过如果需要你可以使用硬编码类替换。

创建后重定向

使用 successRedirectUrl() 方法中,你可以对表单提交后的重定向进行自定义设置:

CreateAction::make()
->successRedirectUrl(route('posts.list'))

如果你想要使用创建后的记录进行重定向,请使用 $record 参数:

use Illuminate\Database\Eloquent\Model;
 
CreateAction::make()
->successRedirectUrl(fn (Model $record): string => route('posts.edit', [
'post' => $record,
]))

创建保存通知

记录成功创建后,会派送通知给用户,说明操作成功:

要自定义通知标题,请使用 successNotificationTitle() 方法:

CreateAction::make()
->successNotificationTitle('User registered')

使用 successNotification() 方法,你可以自定义整个通知:

use Filament\Notifications\Notification;
 
CreateAction::make()
->successNotification(
Notification::make()
->success()
->title('User registered')
->body('The user has been created successfully.'),
)

要完全禁用通知,请使用 successNotification(null) 方法:

CreateAction::make()
->successNotification(null)

生命周期钩子

钩子可用在 Action 生命周期的各种节点中执行代码,比如表单数据保存之前。

可用的钩子有:

CreateAction::make()
->beforeFormFilled(function () {
// 表单字段以默认值填充之前运行
})
->afterFormFilled(function () {
// 表单字段以默认值填充之后运行
})
->beforeFormValidated(function () {
// 在表单提交时,字段验证之前运行
})
->afterFormValidated(function () {
// 在表单提交时,字段验证之后运行
})
->before(function () {
//表单字段保存到数据库之前
})
->after(function () {
//表单字段保存到数据库之后
})

中断创建处理过程

在任何时候,你都可以在生命周期钩子或者 mutation 方法中,调用 $action->halt(),中断整个创建处理过程:

use Filament\Notifications\Actions\Action;
use Filament\Notifications\Notification;
 
CreateAction::make()
->before(function (CreateAction $action) {
if (! $this->record->team->subscribed()) {
Notification::make()
->warning()
->title('You don\'t have an active subscription!')
->body('Choose a plan to continue.')
->persistent()
->actions([
Action::make('subscribe')
->button()
->url(route('subscribe'), shouldOpenInNewTab: true),
])
->send();
$action->halt();
}
})

如果你想同时关闭 Action 模态框,你可以使用 cancel() 完全取消该 Action:

$action->cancel();

使用 wizard

你可以轻松将创建处理过程转换成多步的向导(Wizard)。将 form() 换成 step(),并传入 Step 对象数组定义步骤:

use Filament\Forms\Components\MarkdownEditor;
use Filament\Forms\Components\TextInput;
use Filament\Forms\Components\Toggle;
use Filament\Forms\Components\Wizard\Step;
 
CreateAction::make()
->steps([
Step::make('Name')
->description('Give the category a unique name')
->schema([
TextInput::make('name')
->required()
->live()
->afterStateUpdated(fn ($state, callable $set) => $set('slug', Str::slug($state))),
TextInput::make('slug')
->disabled()
->required()
->unique(Category::class, 'slug'),
])
->columns(2),
Step::make('Description')
->description('Add some extra details')
->schema([
MarkdownEditor::make('description'),
]),
Step::make('Visibility')
->description('Control who can view it')
->schema([
Toggle::make('is_visible')
->label('Visible to customers.')
->default(true),
]),
])

现在,你就可以创建新纪录,以查看 Action 中的 Wizard。编辑仍然使用资源类定义的表单。

如果你想允许自由导航,使所有步骤都可以跳过,请使用 skippableSteps() 方法:

CreateAction::make()
->steps([
// ...
])
->skippableSteps()

禁用 create another

如果你想移除 "create another" 按钮,请使用 createAnother(false) 方法:

CreateAction::make()
->createAnother(false)
Edit on GitHub

Still need help? Join our Discord community or open a GitHub discussion