Skip to content
全部文档

编辑操作

简介

Filament 包含一个用于编辑 Eloquent 记录的操作。点击触发按钮后,会打开带表单的模态框。用户填写表单后,数据会经过验证并保存到数据库。你可以这样使用:

php
use Filament\Actions\EditAction;
use Filament\Forms\Components\TextInput;

EditAction::make()
    ->schema([
        TextInput::make('title')
            ->required()
            ->maxLength(255),
        // ...
    ])
编辑操作模态框编辑操作模态框

在填充表单前自定义数据

你可能希望在将记录数据填入表单之前先进行修改。为此,可使用 mutateRecordDataUsing() 方法修改 $data 数组,并返回修改后的版本,再填充到表单中:

php
use Filament\Actions\EditAction;

EditAction::make()
    ->mutateRecordDataUsing(function (array $data): array {
        $data['user_id'] = auth()->id();

        return $data;
    })

TIP

除了 $data 之外,mutateRecordDataUsing() 函数还可以注入各种实用工具作为参数。

WARNING

Filament 使用记录的数组表示来填充表单,该数据会作为 Livewire 请求的一部分发送到浏览器。若模型中有包含非有效 UTF-8 二进制数据的列(例如 geometrypointblob 列),则无法序列化为 JSON,模态框将无法打开,且 Laravel 日志中通常不会出现错误。

要解决此问题,请将该列加入模型的 $hidden 数组,以将其从模型的数组和 JSON 表示中排除:

php
protected $hidden = ['location'];

这同样适用于 查看复制 操作。

保存前自定义数据

有时,你可能希望在最终保存到数据库之前修改表单数据。为此,可使用 mutateDataUsing() 方法;该方法可访问作为数组的 $data,并返回修改后的版本:

php
use Filament\Actions\EditAction;

EditAction::make()
    ->mutateDataUsing(function (array $data): array {
        $data['last_edited_by_id'] = auth()->id();

        return $data;
    })

TIP

除了 $data 之外,mutateDataUsing() 函数还可以注入各种实用工具作为参数。

自定义保存过程

你可以使用 using() 方法调整记录的更新方式:

php
use Filament\Actions\EditAction;
use Illuminate\Database\Eloquent\Model;

EditAction::make()
    ->using(function (Model $record, array $data): Model {
        $record->update($data);

        return $record;
    })

TIP

除了 $record$data 之外,using() 函数还可以注入各种实用工具作为参数。

保存后重定向

你可以使用 successRedirectUrl() 方法在表单提交后设置自定义重定向:

php
use Filament\Actions\EditAction;

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

若要根据该记录进行重定向,请使用 $record 参数:

php
use Filament\Actions\EditAction;
use Illuminate\Database\Eloquent\Model;

EditAction::make()
    ->successRedirectUrl(fn (Model $record): string => route('posts.view', [
        'post' => $record,
    ]))

TIP

除了 $record 之外,successRedirectUrl() 函数还可以注入各种实用工具作为参数。

自定义保存通知

记录成功更新后,会向用户发送通知,提示操作成功。

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

php
use Filament\Actions\EditAction;

EditAction::make()
    ->successNotificationTitle('User updated')

TIP

除了允许静态值外,successNotificationTitle() 方法也接受一个函数来动态计算值。你可以将各种实用工具作为参数注入该函数。

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

php
use Filament\Actions\EditAction;
use Filament\Notifications\Notification;

EditAction::make()
    ->successNotification(
       Notification::make()
            ->success()
            ->title('User updated')
            ->body('The user has been saved successfully.'),
    )

TIP

除了允许静态值外,successNotification() 方法也接受一个函数来动态计算值。你可以将各种实用工具作为参数注入该函数。

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

php
use Filament\Actions\EditAction;

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

生命周期钩子

可以使用钩子在操作生命周期的不同节点执行代码,例如在表单保存之前。

可用钩子如下:

php
use Filament\Actions\EditAction;

EditAction::make()
    ->beforeFormFilled(function () {
        // Runs before the form fields are populated from the database.
    })
    ->afterFormFilled(function () {
        // Runs after the form fields are populated from the database.
    })
    ->beforeFormValidated(function () {
        // Runs before the form fields are validated when the form is saved.
    })
    ->afterFormValidated(function () {
        // Runs after the form fields are validated when the form is saved.
    })
    ->before(function () {
        // Runs before the form fields are saved to the database.
    })
    ->after(function () {
        // Runs after the form fields are saved to the database.
    })

TIP

这些钩子函数可以注入各种实用工具作为参数。

中止保存过程

你可以随时在生命周期钩子或变更方法中调用 $action->halt(),这将中止整个保存过程:

php
use App\Models\Post;
use Filament\Actions\Action;
use Filament\Actions\EditAction;
use Filament\Notifications\Notification;

EditAction::make()
    ->before(function (EditAction $action, Post $record) {
        if (! $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();
        }
    })

若还希望关闭操作模态框,可以改用 cancel() 完全取消操作,而不是仅中止:

php
$action->cancel();