您可能希望修改来自一条记录的数据,在它被填充到表单之前。为此,您可以在 Edit 页面类上定义一个 mutateFormDataBeforeFill() 方法,来修改 $data 数组,并在其被填充到表单之前返回修改后的版本:
protected function mutateFormDataBeforeFill(array $data): array
{
$data['user_id'] = auth()->id();
return $data;
}或者,如果你正在模态操作中编辑记录,请查看操作文档。
有时,你可能希望在表单数据最终保存到数据库之前对其进行修改。为此,你可以在编辑页面类上定义一个 mutateFormDataBeforeSave() 方法,该方法接受 $data 作为一个数组,并返回修改后的数据:
protected function mutateFormDataBeforeSave(array $data): array
{
$data['last_edited_by_id'] = auth()->id();
return $data;
}另外,如果你正在模态操作中编辑记录,请查阅操作文档。
您可以通过编辑页类上的 handleRecordUpdate() 方法,调整记录的更新方式:
use Illuminate\Database\Eloquent\Model;
protected function handleRecordUpdate(Model $record, array $data): Model
{
$record->update($data);
return $record;
}或者,如果您正在模态操作中编辑记录,请查看操作文档。
默认情况下,保存表单不会将用户重定向到另一个页面。
你可以通过覆盖编辑页面类上的 getRedirectUrl() 方法,在表单保存时设置一个自定义重定向。
例如,该表单可以重定向回该资源的列表页:
protected function getRedirectUrl(): string
{
return $this->getResource()::getUrl('index');
}或者 查看页面:
protected function getRedirectUrl(): string
{
return $this->getResource()::getUrl('view', ['record' => $this->getRecord()]);
}如果您希望重定向到上一页,否则重定向到索引页:
protected function getRedirectUrl(): string
{
return $this->previousUrl ?? $this->getResource()::getUrl('index');
}你还可以使用 配置 来一次性自定义所有资源的默认重定向页面:
use Filament\Panel;
public function panel(Panel $panel): Panel
{
return $panel
// ...
->resourceEditPageRedirect('index') // or
->resourceEditPageRedirect('view');
}当记录成功更新时,会向用户发送一条通知,表明其操作已成功。
要自定义此通知的标题,在编辑页面类上定义一个 getSavedNotificationTitle() 方法:
protected function getSavedNotificationTitle(): ?string
{
return 'User updated';
}或者,如果您正在模态操作中编辑记录,请查阅操作文档。
您可以自定义整个通知,通过覆盖编辑页面类上的 getSavedNotification() 方法:
use Filament\Notifications\Notification;
protected function getSavedNotification(): ?Notification
{
return Notification::make()
->success()
->title('User updated')
->body('The user has been saved successfully.');
}要完全禁用此通知,请从编辑页面类上的 getSavedNotification() 方法返回 null:
use Filament\Notifications\Notification;
protected function getSavedNotification(): ?Notification
{
return null;
}钩子可用于在页面的生命周期内的不同时刻执行代码,例如在表单保存之前。要设置钩子,请在 Edit 页面类上创建一个受保护方法,并以钩子的名称命名:
protected function beforeSave(): void
{
// ...
}在此示例中,beforeSave() 方法中的代码将在表单数据保存到数据库之前被调用。
针对编辑页面,有几个可用的钩子:
use Filament\Resources\Pages\EditRecord;
class EditUser extends EditRecord
{
// ...
protected function beforeFill(): void
{
// Runs before the form fields are populated from the database.
}
protected function afterFill(): void
{
// Runs after the form fields are populated from the database.
}
protected function beforeValidate(): void
{
// Runs before the form fields are validated when the form is saved.
}
protected function afterValidate(): void
{
// Runs after the form fields are validated when the form is saved.
}
protected function beforeSave(): void
{
// Runs before the form fields are saved to the database.
}
protected function afterSave(): void
{
// Runs after the form fields are saved to the database.
}
}或者,如果你正在模态操作中编辑记录,请查阅操作文档。
您可能希望允许用户独立于表单的其余部分保存表单的一部分。一种方法是使用页眉或页脚中的节操作。从 action() 方法中,您可以调用 saveFormComponentOnly(),传入您想要保存的 Section 组件:
use Filament\Actions\Action;
use Filament\Notifications\Notification;
use Filament\Resources\Pages\EditRecord;
use Filament\Schemas\Components\Section;
Section::make('Rate limiting')
->schema([
// ...
])
->footerActions([
fn (string $operation): Action => Action::make('save')
->action(function (Section $component, EditRecord $livewire) {
$livewire->saveFormComponentOnly($component);
Notification::make()
->title('Rate limiting saved')
->body('The rate limiting settings have been saved successfully.')
->success()
->send();
})
->visible($operation === 'edit'),
])可以使用 $operation 辅助函数,以确保该操作仅在表单编辑时可见。
在任何时候,您可以从生命周期钩子或变异方法内部调用 $this->halt(),这将停止整个保存过程:
use Filament\Actions\Action;
use Filament\Notifications\Notification;
protected function beforeSave(): void
{
if (! $this->getRecord()->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();
$this->halt();
}
}或者,如果你正在模态操作中编辑记录,请查阅操作文档。
为了授权,Filament 将会观察任何 模型策略 在你的应用中注册的。
用户可以访问编辑页面,如果模型策略的 update() 方法返回 true。
他们也有能力删除记录,如果策略的 delete() 方法返回 true。
"操作" 是显示在页面上的按钮,允许用户在页面上运行 Livewire 方法或访问 URL。
在资源页面上,操作通常位于两个位置:在页面的右上角,以及在表单下方。
例如,您可以在编辑页面上"删除"旁边添加一个新的按钮操作:
use Filament\Actions;
use Filament\Resources\Pages\EditRecord;
class EditUser extends EditRecord
{
// ...
protected function getHeaderActions(): array
{
return [
Actions\Action::make('impersonate')
->action(function (): void {
// ...
}),
Actions\DeleteAction::make(),
];
}
}或者, 一个新按钮在 "保存" 旁边在表单下方:
use Filament\Actions\Action;
use Filament\Resources\Pages\EditRecord;
class EditUser extends EditRecord
{
// ...
protected function getFormActions(): array
{
return [
...parent::getFormActions(),
Action::make('close')->action('saveAndClose'),
];
}
public function saveAndClose(): void
{
// ...
}
}要查看完整的 actions API,请访问页面部分。
通过覆盖 getHeaderActions() 方法并使用 getSaveFormAction(),可以将“保存”按钮添加到页面头部。你需要将 formId() 传递给该操作,以指定该操作应提交 ID 为 form 的表单,该 form ID 是页面视图中使用的 <form> ID:
protected function getHeaderActions(): array
{
return [
$this->getSaveFormAction()
->formId('form'),
];
}您可以从表单中移除所有操作,方法是重写 getFormActions() 方法,使其返回一个空数组:
protected function getFormActions(): array
{
return [];
}一个编辑页面可能不足以容纳用户浏览多个表单字段。您可以为一个资源创建任意数量的编辑页面。如果您正在使用资源子导航,那么这尤其有用,因为您可以轻松地在不同的编辑页面之间切换。
要创建编辑页面,您应该使用 make:filament-page 命令:
php artisan make:filament-page EditCustomerContact --resource=CustomerResource --type=EditRecord您必须在您的资源中注册这个新页面getPages()方法:
public static function getPages(): array
{
return [
'index' => Pages\ListCustomers::route('/'),
'create' => Pages\CreateCustomer::route('/create'),
'view' => Pages\ViewCustomer::route('/{record}'),
'edit' => Pages\EditCustomer::route('/{record}/edit'),
'edit-contact' => Pages\EditCustomerContact::route('/{record}/edit/contact'),
];
}现在,你可以为该页面定义 form(),它可以包含主编辑页面上不存在的其他字段:
use Filament\Schemas\Schema;
public function form(Schema $schema): Schema
{
return $schema
->components([
// ...
]);
}如果您正在使用资源子导航,您可以在资源的getRecordSubNavigation()中正常注册此页面:
use App\Filament\Resources\Customers\Pages;
use Filament\Resources\Pages\Page;
public static function getRecordSubNavigation(Page $page): array
{
return $page->generateNavigationItems([
// ...
Pages\EditCustomerContact::class,
]);
}Filament 中的每个页面都有自己的 页面结构,它定义了整体结构和内容。你可以通过在该页面上定义一个 content() 方法来覆盖页面的页面结构。默认情况下,编辑页面的 content() 方法包含以下组件:
use Filament\Schemas\Schema;
public function content(Schema $schema): Schema
{
return $schema
->components([
$this->getFormContentComponent(), // This method returns a component to display the form that is defined in this resource
$this->getRelationManagersContentComponent(), // This method returns a component to display the relation managers that are defined in this resource
]);
}在 components() 数组中,你可以插入任何 schema 组件。 你可以通过改变数组的顺序来重新排序这些组件,或移除任何不需要的组件。
为了进一步的自定义机会,你可以在页面类上覆盖静态的$view属性,将其设置为你应用中的一个自定义视图:
protected string $view = 'filament.resources.users.pages.edit-user';这假设您已在 resources/views/filament/resources/users/pages/edit-user.blade.php 创建了一个视图:
<x-filament-panels::page>
{{-- `$this->getRecord()` will return the current Eloquent record for this page --}}
{{ $this->content }} {{-- This will render the content of the page defined in the `content()` method, which can be removed if you want to start from scratch --}}
</x-filament-panels::page>