Actions
概述
简介
“Action(操作)” 是 Laravel 社区使用较多的一个词。传统上,PHP 操作类在应用业务逻辑中处理”执行”动作。比如,登录一个用户、发送邮件或者在数据库中创建新用户记录。
在 Filament 中,Action 也在应用中处理“执行”动作。不过,与传统的 Action 有些不同。它被设计用于用户界面的上下文中。比如,你可能有一个按钮用来删除客户记录,它会打开模态框让你确认删除。当用户点击模态框中的“删除”按钮后,客户就被删除了。这一整个工作流程就是一个 “Action”。
use Filament\Actions\Action;
Action::make('delete')
->requiresConfirmation()
->action(fn () => $this->client->delete())
Action 也能从用户那边收集额外信息。比如,你可以有一个按钮用来发送邮件给客户。当用户点击按钮时,打开模态框获取邮件标题和内容。当用户点击模态框中的“发送”按钮后,就会发送邮件:
use Filament\Actions\Action;
use Filament\Forms\Components\RichEditor;
use Filament\Forms\Components\TextInput;
use Illuminate\Support\Facades\Mail;
Action::make('sendEmail')
->schema([
TextInput::make('subject')->required(),
RichEditor::make('body')->required(),
])
->action(function (array $data) {
Mail::to($this->client)
->send(new GenericEmail(
subject: $data['subject'],
body: $data['body'],
));
})
除了 $data
之外,action()
函数可以注入各种 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. |
通常,Action 的执行不会让用户重定向离开当前页面。这是因为我们广泛使用了 Livewire。不过,Action 也可以更简单,甚至不需要模态框。你可以传递一个 URL 给 Action,当用户点击按钮时,他们就会重定向到那个页面:
use Filament\Actions\Action;
Action::make('edit')
->url(fn (): string => route('posts.edit', ['post' => $this->post]))
除了允许静态值之外,url()
方法也可以接受函数来动态计算其值。你可以将各种 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. |
Action 触发按钮和模态框的整体外观可以使用 Fluent PHP 方法自定义。我们为 UI 提供了一个合理且一致的样式,不过所有这些都可以使用 CSS 自定义。
可用 Action
Filament 包含多个 Action,你可以将它们添加到你的应用中。它们旨在简化最通用的 Eloquent 相关操作:
你也可以创建自己的 Action 来做任何事情,这些只是我们引入的开箱即用的常用 Action。
选择触发样式
Action 触发器有 4 种开箱即用的样式 - “按钮”、“链接”、“图标按钮” 以及 “徽章”。
“按钮”触发器有背景色、标签,以及图标可选。通常这是默认的按钮样式,但你也可以使用 button()
方法手动设置:
use Filament\Actions\Action;
Action::make('edit')
->button()

“链接”触发器没有背景色。但必须有标签,以及图标可选。它是一个你有嵌套文本的链接。你可以使用 link()
方法切换成此样式:
use Filament\Actions\Action;
Action::make('edit')
->link()

“图标按钮”触发器是带有图标,没有标签的圆形按钮。你可以使用 iconButton()
方法切换到该样式:
use Filament\Actions\Action;
Action::make('edit')
->icon('heroicon-m-pencil-square')
->iconButton()

“徽章”触发器有背景色、标签,以及可选的图标。你可以使用 badge()
方法使用徽章作为触发器:
use Filament\Actions\Action;
Action::make('edit')
->badge()

在移动端只使用图标按钮
你可能会在桌面端使用带标签的按钮样式,不过在移动端删除标签。这就将其转换成了图标按钮。你可以使用 labeledFrom()
方法, 传入你希望添加标签的响应式临界点来实现该功能。
use Filament\Actions\Action;
Action::make('edit')
->icon('heroicon-m-pencil-square')
->button()
->labeledFrom('md')
设置图标
默认情况下, 触发按钮的标签是由其名称生成。你也可以使用 label()
方法自定义标签:
use Filament\Actions\Action;
Action::make('edit')
->label('Edit post')
->url(fn (): string => route('posts.edit', ['post' => $this->post]))
除了允许静态值之外,label()
方法也可以接受函数来动态计算其值。你可以将各种 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. |
设置颜色
按钮有一个颜色来指示它们的重要性:
use Filament\Actions\Action;
Action::make('delete')
->color('danger')
除了允许静态值之外,color()
方法也可以接受函数来动态计算其值。你可以将各种 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. |

设置大小
按钮有三个尺寸 - Size::Small
、Size::Medium
或者 Size::Large
。使用 size()
方法可以修改操作触发器的大小:
use Filament\Actions\Action;
use Filament\Support\Enums\Size;
Action::make('create')
->size(Size::Large)
除了允许静态值之外,size()
方法也可以接受函数来动态计算其值。你可以将各种 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. |

设置图标
按钮可能有一个图标,用来添加更多细节到 UI 中。你可以使用 icon()
方法设置图标:
use Filament\Actions\Action;
Action::make('edit')
->url(fn (): string => route('posts.edit', ['post' => $this->post]))
->icon('heroicon-m-pencil-square')
除了允许静态值之外,icon()
方法也可以接受函数来动态计算其值。你可以将各种 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. |

你可以使用 iconPosition()
方法修改图标的位置使之放在标签之后:
use Filament\Actions\Action;
use Filament\Support\Enums\IconPosition;
Action::make('edit')
->url(fn (): string => route('posts.edit', ['post' => $this->post]))
->icon('heroicon-m-pencil-square')
->iconPosition(IconPosition::After)
除了允许静态值之外,iconPosition()
方法也可以接受函数来动态计算其值。你可以将各种 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. |

授权
你可以条件性显示或隐藏特定用户的操作。为此,可以使用 visible()
或 hidden()
方法:
use Filament\Actions\Action;
Action::make('edit')
->url(fn (): string => route('posts.edit', ['post' => $this->post]))
->visible(auth()->user()->can('update', $this->post))
Action::make('edit')
->url(fn (): string => route('posts.edit', ['post' => $this->post]))
->hidden(! auth()->user()->can('update', $this->post))
这个功能可用于授权特定操作给有权限的用户。.
除了允许静态值之外,visible()
和 hidden()
方法也可以接受函数来动态计算它们的值。你可以将各种 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. |
使用策略授权
你可以使用策略来授权某个操作。为此,请将策略方法的名称传递给 authorize()
方法,Filament 将使用该操作的当前 Eloquent 模型来查找对应的策略:
use Filament\Actions\Action;
Action::make('edit')
->url(fn (): string => route('posts.edit', ['post' => $this->post]))
->authorize('update')
NOTE
如果你在面板资源或关联管理器中使用操作,则无需使用 authorize()
方法,因为 Filament 会根据资源模型自动读取内置操作(例如 CreateAction
、EditAction
和 DeleteAction
)的策略。更多信息,请访问资源授权相关文档。
如果你的策略方法返回的是响应消息,你可以禁用该操作而不是隐藏它,并使用 authorizationTooltip()
方法添加包含该消息的 Tooltip:
use Filament\Actions\Action;
Action::make('edit')
->url(fn (): string => route('posts.edit', ['post' => $this->post]))
->authorize('update')
->authorizationTooltip()
你可以改为允许即使用户未获得授权仍可点击该操作,但使用 authorizationNotification()
方法发送包含响应消息的通知:
use Filament\Actions\Action;
Action::make('edit')
->url(fn (): string => route('posts.edit', ['post' => $this->post]))
->authorize('update')
->authorizationNotification()
禁用按钮
如果想要禁用按钮而非隐藏按钮,你可以使用 disabled()
方法:
use Filament\Actions\Action;
Action::make('delete')
->disabled()
你可以传入一个布尔值条件性地禁用按钮:
use Filament\Actions\Action;
Action::make('delete')
->disabled(! auth()->user()->can('delete', $this->post))
除了允许静态值之外,disabled()
方法也可以接受函数来动态计算其值。你可以将各种 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. |
注册键盘绑定
你可以将键盘快捷键附加到触发按钮上。这一行为使用与 Mousetrap 一样的 Key Code:
use Filament\Actions\Action;
Action::make('save')
->action(fn () => $this->save())
->keyBindings(['command+s', 'ctrl+s'])
除了允许静态值之外,keyBindings()
方法也可以接受函数来动态计算其值。你可以将各种 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. |
添加徽章到按钮的角上
你可以添加徽章到按钮的角上,显示任何你想展示的。比如,统计数或状态指示器:
use Filament\Actions\Action;
Action::make('filter')
->iconButton()
->icon('heroicon-m-funnel')
->badge(5)
除了允许静态值之外,badge()
方法也可以接受函数来动态计算其值。你可以将各种 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. |

你也可以传入一个用于徽章的颜色:
use Filament\Actions\Action;
Action::make('filter')
->iconButton()
->icon('heroicon-m-funnel')
->badge(5)
->badgeColor('success')
除了允许静态值之外,badgeColor()
方法也可以接受函数来动态计算其值。你可以将各种 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. |

按钮样式轮廓
使用“按钮”触发器样式时,你可能希望使其不那么突出。你可以使用不同颜色,不过有时你会希望使其轮廓分明。你可以使用 outlined()
方法实现该功能:
use Filament\Actions\Action;
Action::make('edit')
->url(fn (): string => route('posts.edit', ['post' => $this->post]))
->button()
->outlined()

Optionally, you may pass a boolean value to control if the label should be hidden or not:
use Filament\Actions\Action;
Action::make('edit')
->url(fn (): string => route('posts.edit', ['post' => $this->post]))
->button()
->outlined(FeatureFlag::active())
除了允许静态值之外,outlined()
方法也可以接受函数来动态计算其值。你可以将各种 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. |
添加额外的 HTML 属性
你可以传递额外的 HTML 属性给按钮,使其合并到外部 DOM 元素。把属性名作为数组键、属性值作为数组值,将一个属性数组传递给 extraAttributes()
方法:
use Filament\Actions\Action;
Action::make('edit')
->url(fn (): string => route('posts.edit', ['post' => $this->post]))
->extraAttributes([
'title' => 'Edit this post',
])
除了允许静态值之外,extraAttributes()
方法也可以接受函数来动态计算其值。你可以将各种 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. |
TIP
默认情况下,多次调用 extraAttributes()
将覆盖之前的属性。如果你希望合并属性,可以向该方法传递 merge: true
。
限流 Action
通过使用 rateLimit()
方法,你可以对操作进行限流。该方法接受一个用于限制用户 IP 每分钟尝试访问次数的数字。如果用户超过该限制,那么操作不会运行,同时会显示一条通知:
use Filament\Actions\Action;
Action::make('delete')
->rateLimit(5)
如果操作打开了模态框,那么限流会在模态框提交时生效。
如果某个操作使用参数或针对特定的 Eloquent 记录打开,则限流将应用于每个操作的每个唯一参数组合或记录。限流也适用于面板中当前的 Livewire 组件/页面。
除了允许静态值之外,rateLimit()
方法也可以接受函数来动态计算其值。你可以将各种 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. |
自定义限流通知
当 Action 限流时,会发送通知给用户,以说明限流。
要自定义通知标题,请使用 rateLimitedNotificationTitle()
方法:
use Filament\Actions\DeleteAction;
DeleteAction::make()
->rateLimit(5)
->rateLimitedNotificationTitle('Slow down!')
除了允许静态值之外,rateLimitedNotificationTitle()
方法也可以接受函数来动态计算其值。你可以将各种 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. |
使用 rateLimitedNotification()
方法,你可以自定义整个通知:
use DanHarrin\LivewireRateLimiting\Exceptions\TooManyRequestsException;
use Filament\Actions\DeleteAction;
use Filament\Notifications\Notification;
DeleteAction::make()
->rateLimit(5)
->rateLimitedNotification(
fn (TooManyRequestsException $exception): Notification => Notification::make()
->warning()
->title('Slow down!')
->body("You can try deleting again in {$exception->secondsUntilAvailable} seconds."),
)
除了允许静态值之外,rateLimitedNotification()
方法也可以接受函数来动态计算其值。你可以将各种 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. |
Exception | DanHarrin\LivewireRateLimiting\Exceptions\TooManyRequestsException | $exception | The exception encountered when the rate limit was hit. |
Livewire | Livewire\Component | $livewire | The Livewire component instance. |
Minutes until available | int | $minutes | The number of minutes until the rate limit will pass. |
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 for the rate limit, 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. |
Seconds until available | int | $seconds | The number of seconds until the rate limit will pass. |
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. |
自定义限流行为
如果你想自定义限流行为,你可以在 Aciton 中将 Laravel 的 rate limiting 特性和 Filament 的 flash 通知一起使用。
如果你想在 Action 模态框打开时,立即进行限流,你可以在 mountUsing()
方法中实现:
use Filament\Actions\Action;
use Filament\Notifications\Notification;
use Illuminate\Support\Facades\RateLimiter;
Action::make('delete')
->mountUsing(function () {
if (RateLimiter::tooManyAttempts(
$rateLimitKey = 'delete:' . auth()->id(),
maxAttempts: 5,
)) {
Notification::make()
->title('Too many attempts')
->body('Please try again in ' . RateLimiter::availableIn($rateLimitKey) . ' seconds.')
->danger()
->send();
return;
}
RateLimiter::hit($rateLimitKey);
})
如果你想在 Action 运行时限流,你可以在 action()
方法中实现:
use Filament\Actions\Action;
use Filament\Notifications\Notification;
use Illuminate\Support\Facades\RateLimiter;
Action::make('delete')
->action(function () {
if (RateLimiter::tooManyAttempts(
$rateLimitKey = 'delete:' . auth()->id(),
maxAttempts: 5,
)) {
Notification::make()
->title('Too many attempts')
->body('Please try again in ' . RateLimiter::availableIn($rateLimitKey) . ' seconds.')
->danger()
->send();
return;
}
RateLimiter::hit($rateLimitKey);
// ...
})
Action utility 注入
用于配置 Action 的大部分方法都接受函数作为参数,而不只是硬编码值:
use Filament\Actions\Action;
Action::make('edit')
->label('Edit post')
->url(fn (): string => route('posts.edit', ['post' => $this->post]))
这就解锁了许多自定义的可能。
该包也可以在这些函数里面以参数的形式注入许多 utility。所有接受函数作为参数的自定义方法,都可以注入 utility。
这些注入的 utility 需要使用特定的参数名。否则,Filament 无从得知注入的是什么。
注入当前模态框表单数据
如果你想访问当前模态框表单数据,请定义 $data
参数:
function (array $data) {
// ...
}
请注意如果模态框还未提交,该值将为空。
注入 Eloquent 记录
如果 Action 与 Eloquent 记录相关联,比如它在表格行上,你可以使用 $record
参数将记录注入其中:
use Illuminate\Database\Eloquent\Model;
function (Model $record) {
// ...
}
注入当前参数
如果你希望访问当前参数传入到该 Action 的参数,请定义 $arguments
参数:
function (array $arguments) {
// ...
}
注入 Schema 的 utility
如果你的 Action 是在 Schma 中定义的,那么你可以访问多个其他相关 utility:
$schema
- Action 所属的 Schema 实例。$schemaComponent
- Action 所属的 Schema 组件实例。$schemaComponentState
- Schema 组件的当前值。$schemaGet
- 用于从 Schema 数据中检索值的函数。不会在表单字段中运行验证。$schemaSet
- 用于在 Schema 数据中设置值的函数。$schemaOperation
- Schema 执行的当前操作。通常为create
、edit
或view
。
更多详细信息,请访问 Schema 文档。
注入当前 Livewire 组件实例
如果你想访问 Action 从属的当前 Livewire 组件实例,请定义 $livewire
参数:
use Livewire\Component;
function (Component $livewire) {
// ...
}
注入当前 Action 实例
如果你想访问当前 Action 实例,请定义 $action
参数:
function (Action $action) {
// ...
}
注入各种 utility
参数使用反射动态注入,因此,你可以按任何顺序组合使用多个参数:
use Livewire\Component;
function (array $arguments, Component $livewire) {
// ...
}
注入来自 Laravel 容器的依赖
你可以像平常一样注入 Laravel 容器中的其他对象:
use Illuminate\Http\Request;
function (Request $request, array $arguments) {
// ...
}
Edit on GitHubStill need help? Join our Discord community or open a GitHub discussion