Actions
强制删除操作
简介
Filament 包含一个可以强制删除软删除 Eloquent 记录的 Action。点击触发按钮后,会打开一个模态框要求用户确认。你可以这样使用:
use Filament\Actions\ForceDeleteAction;
ForceDeleteAction::make()
或者如果你想将其添加为批量操作,以便用户可以选择强制删除哪些行,请使用 Filament\Actions\ForceDeleteBulkAction
:
use Filament\Actions\ForceDeleteBulkAction;
use Filament\Tables\Table;
public function table(Table $table): Table
{
return $table
->toolbarActions([
ForceDeleteBulkAction::make(),
]);
}
强制删除后重定向
使用 successRedirectUrl()
方法,你可以自定义表单提交后的重定向设置:
use Filament\Actions\ForceDeleteAction;
ForceDeleteAction::make()
->successRedirectUrl(route('posts.list'))
As well as $record
, the successRedirectUrl()
function 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. |
自定义强制删除通知
记录强制删除成功后,会发送一个通知给用户,说明操作成功:
要自定义通知标题,请使用 successNotificationTitle()
方法:
use Filament\Actions\ForceDeleteAction;
ForceDeleteAction::make()
->successNotificationTitle('User force-deleted')
除了允许静态值之外,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\ForceDeleteAction;
use Filament\Notifications\Notification;
ForceDeleteAction::make()
->successNotification(
Notification::make()
->success()
->title('User force-deleted')
->body('The user has been force-deleted 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\ForceDeleteAction;
ForceDeleteAction::make()
->successNotification(null)
生命周期钩子
你可以使用 before()
和 after()
方法,在记录强制删除之前及之后执行代码:
use Filament\Actions\ForceDeleteAction;
ForceDeleteAction::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. |
改进批量删除操作的性能
默认情况下,ForceDeleteBulkAction
将会将所有 Eloquent 记录加载到内存中,然后循环并逐一删除它们。
如果删除的记录数量太大,你可能会希望使用 chunkSelectedRecords()
方法,一次或许较小的数据量。这能够减少应用的内存使用:
use Filament\Actions\ForceDeleteBulkAction;
ForceDeleteBulkAction::make()
->chunkSelectedRecords(250)
Filament 在删除记录前将它们加载入内存出于两个原因:
- 允许在删除之前使用模型策略对集合中的单个记录进行授权(比如,使用
authorizeIndividualRecords('delete')
策略)。 - 确保在删除记录时运行模型事件,例如模型观察者中的
forceDeleting
和forceDeleted
事件。
如果你不需要单独的记录策略授权和模型事件,则可以使用 fetchSelectedRecords(false)
方法,该方法在删除记录之前不会将其提取到内存中,而是在单次查询中删除它们:
use Filament\Actions\ForceDeleteBulkAction;
ForceDeleteBulkAction::make()
->fetchSelectedRecords(false)
Edit on GitHubStill need help? Join our Discord community or open a GitHub discussion