Action
恢复操作
简介
Filament 包含一个可以恢复已软删除的 Eloquent 记录的 Action。点击触发按钮后,会打开一个模态框要求用户确认。你可以这样使用:
use Filament\Actions\RestoreAction;
RestoreAction::make()
或者,如果你想将其添加入表格的批量操作,以便用户可以选择恢复哪些记录,请使用 Filament\Actions\RestoreBulkAction:
use Filament\Actions\RestoreBulkAction;
use Filament\Tables\Table;
public function table(Table $table): Table
{
return $table
->toolbarActions([
RestoreBulkAction::make(),
]);
}
恢复后重定向
使用 successRedirectUrl() 方法,你可以自定义表单提交后的重定向设置:
use Filament\Actions\RestoreAction;
RestoreAction::make()
->successRedirectUrl(route('posts.list'))
除了 $record 之外,successRedirectUrl() 函数也可以将各种 utility 作为参数注入。
Learn more about utility injection.
| Utility | Type | Parameter | Description |
|---|---|---|---|
| Action | Filament\Actions\Action | $action | The current action instance. |
| Arguments | array<string, mixed> | $arguments | The array of arguments passed to the action when it was triggered. |
| Data | array<string, mixed> | $data | The array of data submitted from form fields in the action's modal. It will be empty before the modal form is submitted. |
| Livewire | Livewire\Component | $livewire | The Livewire component instance. |
| Eloquent model FQN | ?string<Illuminate\Database\Eloquent\Model> | $model | The Eloquent model FQN for the current action, if one is attached. |
| Mounted actions | array<Filament\Actions\Action> | $mountedActions | The array of actions that are currently mounted in the Livewire component. This is useful for accessing data from parent actions. |
| Eloquent record | ?Illuminate\Database\Eloquent\Model | $record | The Eloquent record for the current action, if one is attached. |
| Schema | Filament\Schemas\Schema | $schema | [Actions in schemas only] The schema object that this action belongs to. |
| Schema component | Filament\Schemas\Components\Component | $schemaComponent | [Actions in schemas only] The schema component that this action belongs to. |
| Schema component state | mixed | $schemaComponentState | [Actions in schemas only] The current value of the schema component. |
| Schema get function | Filament\Schemas\Components\Utilities\Get | $schemaGet | [Actions in schemas only] A function for retrieving values from the schema data. Validation is not run on form fields. |
| Schema operation | string | $schemaOperation | [Actions in schemas only] The current operation being performed by the schema. Usually create, edit, or view. |
| Schema set function | Filament\Schemas\Components\Utilities\Set | $schemaSet | [Actions in schemas only] A function for setting values in the schema data. |
| Selected Eloquent records | Illuminate\Support\Collection | $selectedRecords | [Bulk actions only] The Eloquent records selected in the table. |
| Table | Filament\Tables\Table | $table | [Actions in tables only] The table object that this action belongs to. |
自定义恢复通知
记录恢复成功后,会发送一个通知给用户,告知操作成功:
要自定义通知标题,请使用 successNotificationTitle() 方法:
use Filament\Actions\RestoreAction;
RestoreAction::make()
->successNotificationTitle('User restored')
除了允许静态值之外,successNotificationTitle() 方法也可以接受函数动态计算其值。你可以将各种 utility 作为参数注入到函数中。
Learn more about utility injection.
| Utility | Type | Parameter | Description |
|---|---|---|---|
| Action | Filament\Actions\Action | $action | The current action instance. |
| Arguments | array<string, mixed> | $arguments | The array of arguments passed to the action when it was triggered. |
| Data | array<string, mixed> | $data | The array of data submitted from form fields in the action's modal. It will be empty before the modal form is submitted. |
| Livewire | Livewire\Component | $livewire | The Livewire component instance. |
| Eloquent model FQN | ?string<Illuminate\Database\Eloquent\Model> | $model | The Eloquent model FQN for the current action, if one is attached. |
| Mounted actions | array<Filament\Actions\Action> | $mountedActions | The array of actions that are currently mounted in the Livewire component. This is useful for accessing data from parent actions. |
| Eloquent record | ?Illuminate\Database\Eloquent\Model | $record | The Eloquent record for the current action, if one is attached. |
| Schema | Filament\Schemas\Schema | $schema | [Actions in schemas only] The schema object that this action belongs to. |
| Schema component | Filament\Schemas\Components\Component | $schemaComponent | [Actions in schemas only] The schema component that this action belongs to. |
| Schema component state | mixed | $schemaComponentState | [Actions in schemas only] The current value of the schema component. |
| Schema get function | Filament\Schemas\Components\Utilities\Get | $schemaGet | [Actions in schemas only] A function for retrieving values from the schema data. Validation is not run on form fields. |
| Schema operation | string | $schemaOperation | [Actions in schemas only] The current operation being performed by the schema. Usually create, edit, or view. |
| Schema set function | Filament\Schemas\Components\Utilities\Set | $schemaSet | [Actions in schemas only] A function for setting values in the schema data. |
| Selected Eloquent records | Illuminate\Support\Collection | $selectedRecords | [Bulk actions only] The Eloquent records selected in the table. |
| Table | Filament\Tables\Table | $table | [Actions in tables only] The table object that this action belongs to. |
你也可以使用 successNotification() 方法,自定义整个通知:
use Filament\Actions\RestoreAction;
use Filament\Notifications\Notification;
RestoreAction::make()
->successNotification(
Notification::make()
->success()
->title('User restored')
->body('The user has been restored successfully.'),
)
除了允许静态值之外,successNotification() 方法也可以接受函数动态计算其值。你可以将各种 utility 作为参数注入到函数中。
Learn more about utility injection.
| Utility | Type | Parameter | Description |
|---|---|---|---|
| Action | Filament\Actions\Action | $action | The current action instance. |
| Arguments | array<string, mixed> | $arguments | The array of arguments passed to the action when it was triggered. |
| Data | array<string, mixed> | $data | The array of data submitted from form fields in the action's modal. It will be empty before the modal form is submitted. |
| Livewire | Livewire\Component | $livewire | The Livewire component instance. |
| Eloquent model FQN | ?string<Illuminate\Database\Eloquent\Model> | $model | The Eloquent model FQN for the current action, if one is attached. |
| Mounted actions | array<Filament\Actions\Action> | $mountedActions | The array of actions that are currently mounted in the Livewire component. This is useful for accessing data from parent actions. |
| Notification | Filament\Notifications\Notification | $notification | The default notification object, which could be a useful starting point for customization. |
| Eloquent record | ?Illuminate\Database\Eloquent\Model | $record | The Eloquent record for the current action, if one is attached. |
| Schema | Filament\Schemas\Schema | $schema | [Actions in schemas only] The schema object that this action belongs to. |
| Schema component | Filament\Schemas\Components\Component | $schemaComponent | [Actions in schemas only] The schema component that this action belongs to. |
| Schema component state | mixed | $schemaComponentState | [Actions in schemas only] The current value of the schema component. |
| Schema get function | Filament\Schemas\Components\Utilities\Get | $schemaGet | [Actions in schemas only] A function for retrieving values from the schema data. Validation is not run on form fields. |
| Schema operation | string | $schemaOperation | [Actions in schemas only] The current operation being performed by the schema. Usually create, edit, or view. |
| Schema set function | Filament\Schemas\Components\Utilities\Set | $schemaSet | [Actions in schemas only] A function for setting values in the schema data. |
| Selected Eloquent records | Illuminate\Support\Collection | $selectedRecords | [Bulk actions only] The Eloquent records selected in the table. |
| Table | Filament\Tables\Table | $table | [Actions in tables only] The table object that this action belongs to. |
要完全禁用通知,请使用 successNotification(null) 方法:
use Filament\Actions\RestoreAction;
RestoreAction::make()
->successNotification(null)
生命周期钩子
你可以使用 before() 和 after() 方法,在记录恢复之前及之后执行代码:
use Filament\Actions\RestoreAction;
RestoreAction::make()
->before(function () {
// ...
})
->after(function () {
// ...
})
These hook functions can inject various utilities as parameters.
Learn more about utility injection.| Utility | Type | Parameter | Description |
|---|---|---|---|
| Action | Filament\Actions\Action | $action | The current action instance. |
| Arguments | array<string, mixed> | $arguments | The array of arguments passed to the action when it was triggered. |
| Data | array<string, mixed> | $data | The array of data submitted from form fields in the action's modal. It will be empty before the modal form is submitted. |
| Livewire | Livewire\Component | $livewire | The Livewire component instance. |
| Eloquent model FQN | ?string<Illuminate\Database\Eloquent\Model> | $model | The Eloquent model FQN for the current action, if one is attached. |
| Mounted actions | array<Filament\Actions\Action> | $mountedActions | The array of actions that are currently mounted in the Livewire component. This is useful for accessing data from parent actions. |
| Eloquent record | ?Illuminate\Database\Eloquent\Model | $record | The Eloquent record for the current action, if one is attached. |
| Schema | Filament\Schemas\Schema | $schema | [Actions in schemas only] The schema object that this action belongs to. |
| Schema component | Filament\Schemas\Components\Component | $schemaComponent | [Actions in schemas only] The schema component that this action belongs to. |
| Schema component state | mixed | $schemaComponentState | [Actions in schemas only] The current value of the schema component. |
| Schema get function | Filament\Schemas\Components\Utilities\Get | $schemaGet | [Actions in schemas only] A function for retrieving values from the schema data. Validation is not run on form fields. |
| Schema operation | string | $schemaOperation | [Actions in schemas only] The current operation being performed by the schema. Usually create, edit, or view. |
| Schema set function | Filament\Schemas\Components\Utilities\Set | $schemaSet | [Actions in schemas only] A function for setting values in the schema data. |
| Selected Eloquent records | Illuminate\Support\Collection | $selectedRecords | [Bulk actions only] The Eloquent records selected in the table. |
| Table | Filament\Tables\Table | $table | [Actions in tables only] The table object that this action belongs to. |
改进恢复批量操作的性能
默认情况下,RestoreBulkAction 将会将所有 Eloquent 记录加载到内存中,然后循环并逐一恢复。
如果恢复的记录数量太大,你可能会希望使用 chunkSelectedRecords() 方法,一次或许较小的数据量。这能够减少应用的内存使用:
use Filament\Actions\RestoreBulkAction;
RestoreBulkAction::make()
->chunkSelectedRecords(250)
Filament 在恢复记录前将它们加载入内存出于两个原因:
- 允许在恢复之前使用模型策略对集合中的单个记录进行授权(比如,使用
authorizeIndividualRecords('restore')策略)。 - 确保在恢复记录时运行模型事件,例如模型观察者中的
restoring和restoring事件。
如果你不需要单独的记录策略授权和模型事件,则可以使用 fetchSelectedRecords(false) 方法,该方法在恢复记录之前不会将其提取到内存中,而是在一次查询中恢复它们:
use Filament\Actions\RestoreBulkAction;
RestoreBulkAction::make()
->fetchSelectedRecords(false)
Edit on GitHubStill need help? Join our Discord community or open a GitHub discussion