跳到主要内容
版本:4.x

删除操作

简介

Filament 包含一个能够删除 Eloquent 记录的操作。点击触发按钮时,会打开一个请求用户确认模态框。你可以像这样使用它:

use Filament\Actions\DeleteAction;

DeleteAction::make()

或者,如果你想添加表格批量操作按钮,以便用户可以选择删除哪些行,请使用 Filament\Actions\DeleteBulkAction

use Filament\Actions\DeleteBulkAction;
use Filament\Tables\Table;

public function table(Table $table): Table
{
return $table
->toolbarActions([
DeleteBulkAction::make(),
]);
}

删除后重定向

使用 successRedirectUrl() 方法你可以设置删除记录后的重定向网址:

use Filament\Actions\DeleteAction;

DeleteAction::make()
->successRedirectUrl(route('posts.list'))
除了 $record 之外,successRedirectUrl() 函数可以注入各种 utility 作为参数。Learn more about utility injection.
UtilityTypeParameterDescription
ActionFilament\Actions\Action$actionThe current action instance.
Argumentsarray<string, mixed>$argumentsThe array of arguments passed to the action when it was triggered.
Dataarray<string, mixed>$dataThe array of data submitted from form fields in the action's modal. It will be empty before the modal form is submitted.
LivewireLivewire\Component$livewireThe Livewire component instance.
Eloquent model FQN?string<Illuminate\Database\Eloquent\Model>$modelThe Eloquent model FQN for the current action, if one is attached.
Mounted actionsarray<Filament\Actions\Action>$mountedActionsThe 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$recordThe Eloquent record for the current action, if one is attached.
SchemaFilament\Schemas\Schema$schema[Actions in schemas only] The schema object that this action belongs to.
Schema componentFilament\Schemas\Components\Component$schemaComponent[Actions in schemas only] The schema component that this action belongs to.
Schema component statemixed$schemaComponentState[Actions in schemas only] The current value of the schema component.
Schema get functionFilament\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 operationstring$schemaOperation[Actions in schemas only] The current operation being performed by the schema. Usually <code>create</code>, <code>edit</code>, or <code>view</code>.
Schema set functionFilament\Schemas\Components\Utilities\Set$schemaSet[Actions in schemas only] A function for setting values in the schema data.
Selected Eloquent recordsIlluminate\Support\Collection$selectedRecords[Bulk actions only] The Eloquent records selected in the table.
TableFilament\Tables\Table$table[Actions in tables only] The table object that this action belongs to.

自定义删除通知

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

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

use Filament\Actions\DeleteAction;

DeleteAction::make()
->successNotificationTitle('User deleted')
除了允许静态值之外,successNotificationTitle() 方法也可以接受函数动态计算其值。你可以将各种 utility 作为参数注入到函数中。Learn more about utility injection.
UtilityTypeParameterDescription
ActionFilament\Actions\Action$actionThe current action instance.
Argumentsarray<string, mixed>$argumentsThe array of arguments passed to the action when it was triggered.
Dataarray<string, mixed>$dataThe array of data submitted from form fields in the action's modal. It will be empty before the modal form is submitted.
LivewireLivewire\Component$livewireThe Livewire component instance.
Eloquent model FQN?string<Illuminate\Database\Eloquent\Model>$modelThe Eloquent model FQN for the current action, if one is attached.
Mounted actionsarray<Filament\Actions\Action>$mountedActionsThe 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$recordThe Eloquent record for the current action, if one is attached.
SchemaFilament\Schemas\Schema$schema[Actions in schemas only] The schema object that this action belongs to.
Schema componentFilament\Schemas\Components\Component$schemaComponent[Actions in schemas only] The schema component that this action belongs to.
Schema component statemixed$schemaComponentState[Actions in schemas only] The current value of the schema component.
Schema get functionFilament\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 operationstring$schemaOperation[Actions in schemas only] The current operation being performed by the schema. Usually <code>create</code>, <code>edit</code>, or <code>view</code>.
Schema set functionFilament\Schemas\Components\Utilities\Set$schemaSet[Actions in schemas only] A function for setting values in the schema data.
Selected Eloquent recordsIlluminate\Support\Collection$selectedRecords[Bulk actions only] The Eloquent records selected in the table.
TableFilament\Tables\Table$table[Actions in tables only] The table object that this action belongs to.

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

use Filament\Actions\DeleteAction;
use Filament\Notifications\Notification;

DeleteAction::make()
->successNotification(
Notification::make()
->success()
->title('User deleted')
->body('The user has been deleted successfully.'),
)
除了允许静态值之外,successNotification() 方法也可以接受函数动态计算其值。你可以将各种 utility 作为参数注入到函数中。Learn more about utility injection.
UtilityTypeParameterDescription
ActionFilament\Actions\Action$actionThe current action instance.
Argumentsarray<string, mixed>$argumentsThe array of arguments passed to the action when it was triggered.
Dataarray<string, mixed>$dataThe array of data submitted from form fields in the action's modal. It will be empty before the modal form is submitted.
LivewireLivewire\Component$livewireThe Livewire component instance.
Eloquent model FQN?string<Illuminate\Database\Eloquent\Model>$modelThe Eloquent model FQN for the current action, if one is attached.
Mounted actionsarray<Filament\Actions\Action>$mountedActionsThe array of actions that are currently mounted in the Livewire component. This is useful for accessing data from parent actions.
NotificationFilament\Notifications\Notification$notificationThe default notification object, which could be a useful starting point for customization.
Eloquent record?Illuminate\Database\Eloquent\Model$recordThe Eloquent record for the current action, if one is attached.
SchemaFilament\Schemas\Schema$schema[Actions in schemas only] The schema object that this action belongs to.
Schema componentFilament\Schemas\Components\Component$schemaComponent[Actions in schemas only] The schema component that this action belongs to.
Schema component statemixed$schemaComponentState[Actions in schemas only] The current value of the schema component.
Schema get functionFilament\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 operationstring$schemaOperation[Actions in schemas only] The current operation being performed by the schema. Usually <code>create</code>, <code>edit</code>, or <code>view</code>.
Schema set functionFilament\Schemas\Components\Utilities\Set$schemaSet[Actions in schemas only] A function for setting values in the schema data.
Selected Eloquent recordsIlluminate\Support\Collection$selectedRecords[Bulk actions only] The Eloquent records selected in the table.
TableFilament\Tables\Table$table[Actions in tables only] The table object that this action belongs to.

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

use Filament\Actions\DeleteAction;

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

生命周期钩子

你可以使用 before()after() 方法,在记录删除之前及之后执行代码:

use Filament\Actions\DeleteAction;

DeleteAction::make()
->before(function () {
// ...
})
->after(function () {
// ...
})
这些函数可以注入各种 utility 作为参数。Learn more about utility injection.
UtilityTypeParameterDescription
ActionFilament\Actions\Action$actionThe current action instance.
Argumentsarray<string, mixed>$argumentsThe array of arguments passed to the action when it was triggered.
Dataarray<string, mixed>$dataThe array of data submitted from form fields in the action's modal. It will be empty before the modal form is submitted.
LivewireLivewire\Component$livewireThe Livewire component instance.
Eloquent model FQN?string<Illuminate\Database\Eloquent\Model>$modelThe Eloquent model FQN for the current action, if one is attached.
Mounted actionsarray<Filament\Actions\Action>$mountedActionsThe 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$recordThe Eloquent record for the current action, if one is attached.
SchemaFilament\Schemas\Schema$schema[Actions in schemas only] The schema object that this action belongs to.
Schema componentFilament\Schemas\Components\Component$schemaComponent[Actions in schemas only] The schema component that this action belongs to.
Schema component statemixed$schemaComponentState[Actions in schemas only] The current value of the schema component.
Schema get functionFilament\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 operationstring$schemaOperation[Actions in schemas only] The current operation being performed by the schema. Usually <code>create</code>, <code>edit</code>, or <code>view</code>.
Schema set functionFilament\Schemas\Components\Utilities\Set$schemaSet[Actions in schemas only] A function for setting values in the schema data.
Selected Eloquent recordsIlluminate\Support\Collection$selectedRecords[Bulk actions only] The Eloquent records selected in the table.
TableFilament\Tables\Table$table[Actions in tables only] The table object that this action belongs to.

改进批量删除操作的性能

默认情况下,DeleteBulkAction 将会将所有 Eloquent 记录加载到内存中,然后循环并逐一删除它们。

如果删除的记录数量太大,你可能会希望使用 chunkSelectedRecords() 方法,一次或许较小的数据量。这能够减少应用的内存使用:

use Filament\Actions\DeleteBulkAction;

DeleteBulkAction::make()
->chunkSelectedRecords(250)

Filament 在删除记录前将它们加载入内存出于两个原因:

  • 允许在删除之前使用模型策略对集合中的单个记录进行授权(比如,使用 authorizeIndividualRecords('delete') 策略)。
  • 确保在删除记录时运行模型事件,例如模型观察者中的 deletingdeleted 事件。

如果你不需要单独的记录策略授权和模型事件,则可以使用 fetchSelectedRecords(false) 方法,该方法在删除记录之前不会将其提取到内存中,而是在单次查询中删除它们:

use Filament\Actions\DeleteBulkAction;

DeleteBulkAction::make()
->fetchSelectedRecords(false)