表格 - Column
Select column
简介
下拉列表列(SelectColumn)允许你在表格内渲染下拉列表(Select),可用于在不打开新页面或模态框的情况下更新数据库记录:
你必须将选项传入到列中:
use Filament\Tables\Columns\SelectColumn;
SelectColumn::make('status')
->options([
'draft' => 'Draft',
'reviewing' => 'Reviewing',
'published' => 'Published',
])
启用 JavaScript 下拉列表
默认情况下,Filament 使用原生 HTML5 Select 下拉列表。你可以使用 native(false) 方法启用更可定制的 JavaScript Select:
use Filament\Tables\Columns\SelectColumn;
SelectColumn::make('status')
->options([
'draft' => 'Draft',
'reviewing' => 'Reviewing',
'published' => 'Published',
])
->native(false)
As well as allowing a static value, the native() method also accepts a function to dynamically calculate it. You can inject various utilities into the function as parameters.
了解更多 utility 注入详情。 | Utility | 类型 | 参数 | 描述 |
|---|---|---|---|
| Column | Filament\Tables\Columns\Column | $column | The current column instance. |
| Livewire | Livewire\Component | $livewire | The Livewire component instance. |
| Eloquent record | ?Illuminate\Database\Eloquent\Model | $record | The Eloquent record for the current table row. |
| Row loop | stdClass | $rowLoop | The row loop object for the current table row. |
| State | mixed | $state | The current value of the column, based on the current table row. |
| Table | Filament\Tables\Table | $table | The current table instance. |
搜索选项
使用 searchableOptions() 方法,你可以启用搜索输入框,以便更轻松地访问众多选项:
use Filament\Tables\Columns\SelectColumn;
SelectColumn::make('author_id')
->label('Author')
->options(User::query()->pluck('name', 'id'))
->searchableOptions()
此外,你也可以传入一个布尔值,控制输入是否可搜索:
use Filament\Tables\Columns\SelectColumn;
SelectColumn::make('author_id')
->label('Author')
->options(User::query()->pluck('name', 'id'))
->searchableOptions(FeatureFlag::active())
As well as allowing a static value, the searchableOptions() method also accepts a function to dynamically calculate it. You can inject various utilities into the function as parameters.
了解更多 utility 注入详情。 | Utility | 类型 | 参数 | 描述 |
|---|---|---|---|
| Column | Filament\Tables\Columns\Column | $column | The current column instance. |
| Livewire | Livewire\Component | $livewire | The Livewire component instance. |
| Eloquent record | ?Illuminate\Database\Eloquent\Model | $record | The Eloquent record for the current table row. |
| Row loop | stdClass | $rowLoop | The row loop object for the current table row. |
| State | mixed | $state | The current value of the column, based on the current table row. |
| Table | Filament\Tables\Table | $table | The current table instance. |
返回自定义搜索结果
如果你有大量选项,并希望根据数据库搜索或其他外部数据源来填充这些选项,可以使用 getOptionsSearchResultsUsing() 和 getOptionLabelUsing() 方法,而不是 options() 方法。
getOptionsSearchResultsUsing() 方法接受一个回调函数,该函数需返回 $key => $value 格式的搜索结果。当前用户的搜索内容可通过 $search 变量获取,你应该利用它来筛选结果。
getOptionLabelUsing() 方法接受一个回调函数,用于将选定选项的 $value 转换为对应的标签。当表单首次加载且用户尚未进行搜索时,会用到此方法;否则,将无法显示当前选中选项的标签。
若要提供自定义搜索结果,必须同时在选择框(select)上使用 getOptionsSearchResultsUsing() 和 getOptionLabelUsing() 方法:
use Filament\Tables\Columns\SelectColumn;
SelectColumn::make('author_id')
->searchableOptions()
->getOptionsSearchResultsUsing(fn (string $search): array => User::query()
->where('name', 'like', "%{$search}%")
->limit(50)
->pluck('name', 'id')
->all())
->getOptionLabelUsing(fn ($value): ?string => User::find($value)?->name),
getOptionLabelUsing() 至关重要,因为它为 Filament 提供了所选选项的标签,从而避免了执行完整搜索来查找该选项。如果选项无效,则应返回 null。
You can inject various utilities into these functions as parameters.
了解更多 utility 注入详情。| Utility | 类型 | 参数 | 描述 |
|---|---|---|---|
| Column | Filament\Tables\Columns\Column | $column | The current column instance. |
| Livewire | Livewire\Component | $livewire | The Livewire component instance. |
| Eloquent record | ?Illuminate\Database\Eloquent\Model | $record | The Eloquent record for the current table row. |
| Row loop | stdClass | $rowLoop | The row loop object for the current table row. |
| Search | ?string | $search | [getOptionsSearchResultsUsing() only] The current search input value, if the field is searchable. |
| State | mixed | $state | The current value of the column, based on the current table row. |
| Table | Filament\Tables\Table | $table | The current table instance. |
| Option value | mixed | $value | The option value to retrieve the label for. |
设置自定义加载提示信息
在使用可搜索的单选或多选组件时,你可能希望在加载选项期间显示自定义提示信息。你可以使用 optionsLoadingMessage() 方法来实现这一点:
use Filament\Tables\Columns\SelectColumn;
SelectColumn::make('author_id')
->optionsRelationship(name: 'author', titleAttribute: 'name')
->searchableOptions()
->optionsLoadingMessage('Loading authors...')
As well as allowing a static value, the optionsLoadingMessage() method also accepts a function to dynamically calculate it. You can inject various utilities into the function as parameters.
了解更多 utility 注入详情。 | Utility | 类型 | 参数 | 描述 |
|---|---|---|---|
| Column | Filament\Tables\Columns\Column | $column | The current column instance. |
| Livewire | Livewire\Component | $livewire | The Livewire component instance. |
| Eloquent record | ?Illuminate\Database\Eloquent\Model | $record | The Eloquent record for the current table row. |
| Row loop | stdClass | $rowLoop | The row loop object for the current table row. |
| State | mixed | $state | The current value of the column, based on the current table row. |
| Table | Filament\Tables\Table | $table | The current table instance. |
设置自定义的“无搜索结果”提示信息
在使用可搜索的单选或多选组件时,如果未找到搜索结果,你可能希望显示一条自定义提示信息。你可以使用 noOptionsSearchResultsMessage() 方法来实现这一点:
use Filament\Tables\Columns\SelectColumn;
SelectColumn::make('author_id')
->optionsRelationship(name: 'author', titleAttribute: 'name')
->searchableOptions()
->noOptionsSearchResultsMessage('No authors found.')
As well as allowing a static value, the noOptionsSearchResultsMessage() method also accepts a function to dynamically calculate it. You can inject various utilities into the function as parameters.
了解更多 utility 注入详情。 | Utility | 类型 | 参数 | 描述 |
|---|---|---|---|
| Column | Filament\Tables\Columns\Column | $column | The current column instance. |
| Livewire | Livewire\Component | $livewire | The Livewire component instance. |
| Eloquent record | ?Illuminate\Database\Eloquent\Model | $record | The Eloquent record for the current table row. |
| Row loop | stdClass | $rowLoop | The row loop object for the current table row. |
| State | mixed | $state | The current value of the column, based on the current table row. |
| Table | Filament\Tables\Table | $table | The current table instance. |
设置自定义搜索提示语
在使用可搜索的单选或多选组件时,你可能希望在用户尚未输入搜索关键词时显示一条自定义提示信息。你可以通过 optionsSearchPrompt() 方法来实现这一点:
use Filament\Tables\Columns\SelectColumn;
SelectColumn::make('author_id')
->optionsRelationship(name: 'author', titleAttribute: 'name')
->searchableOptions(['name', 'email'])
->optionsSearchPrompt('Search authors by their name or email address')
As well as allowing a static value, the optionsSearchPrompt() method also accepts a function to dynamically calculate it. You can inject various utilities into the function as parameters.
了解更多 utility 注入详情。 | Utility | 类型 | 参数 | 描述 |
|---|---|---|---|
| Column | Filament\Tables\Columns\Column | $column | The current column instance. |
| Livewire | Livewire\Component | $livewire | The Livewire component instance. |
| Eloquent record | ?Illuminate\Database\Eloquent\Model | $record | The Eloquent record for the current table row. |
| Row loop | stdClass | $rowLoop | The row loop object for the current table row. |
| State | mixed | $state | The current value of the column, based on the current table row. |
| Table | Filament\Tables\Table | $table | The current table instance. |
设置自定义搜索消息
当使用可搜索单选或多选时,你可能希望在加载搜索结果时显示自定义消息。你可以使用 optionsSearchingMessage() 方法来执行此操作:
use Filament\Tables\Columns\SelectColumn;
SelectColumn::make('author_id')
->optionsRelationship(name: 'author', titleAttribute: 'name')
->searchableOptions()
->optionsSearchingMessage('Searching authors...')
As well as allowing a static value, the optionsSearchingMessage() method also accepts a function to dynamically calculate it. You can inject various utilities into the function as parameters.
了解更多 utility 注入详情。 | Utility | 类型 | 参数 | 描述 |
|---|---|---|---|
| Column | Filament\Tables\Columns\Column | $column | The current column instance. |
| Livewire | Livewire\Component | $livewire | The Livewire component instance. |
| Eloquent record | ?Illuminate\Database\Eloquent\Model | $record | The Eloquent record for the current table row. |
| Row loop | stdClass | $rowLoop | The row loop object for the current table row. |
| State | mixed | $state | The current value of the column, based on the current table row. |
| Table | Filament\Tables\Table | $table | The current table instance. |
调整搜索防抖设置
默认情况下,当用户在支持搜索的单选或多选组件中输入内容时,Filament 会在执行搜索前等待 1000 毫秒(1 秒)。如果用户持续在搜索框中输入,两次搜索之间也会有 1000 毫秒的等待间隔。你可以使用 optionsSearchDebounce() 方法来修改这一设置:
use Filament\Tables\Columns\SelectColumn;
SelectColumn::make('author_id')
->optionsRelationship(name: 'author', titleAttribute: 'name')
->searchableOptions()
->optionsSearchDebounce(500)
请确保不要将防抖(debounce)时间设置得过短,否则可能会因频繁向服务器发送请求以获取选项,导致选择组件运行缓慢且无响应。
As well as allowing a static value, the optionsSearchDebounce() method also accepts a function to dynamically calculate it. You can inject various utilities into the function as parameters.
了解更多 utility 注入详情。 | Utility | 类型 | 参数 | 描述 |
|---|---|---|---|
| Column | Filament\Tables\Columns\Column | $column | The current column instance. |
| Livewire | Livewire\Component | $livewire | The Livewire component instance. |
| Eloquent record | ?Illuminate\Database\Eloquent\Model | $record | The Eloquent record for the current table row. |
| Row loop | stdClass | $rowLoop | The row loop object for the current table row. |
| State | mixed | $state | The current value of the column, based on the current table row. |
| Table | Filament\Tables\Table | $table | The current table instance. |
结合 Eloquent 关联使用
你可以使用 SelectColumn 的 optionsRelationship() 方法来配置一个 BelongsTo 关联,从而自动获取选项。titleAttribute 指定了用于生成每个选项标签的字段名称:
use Filament\Tables\Columns\SelectColumn;
SelectColumn::make('author_id')
->optionsRelationship(name: 'author', titleAttribute: 'name')
跨多列搜索关联选项
默认情况下,如果选择框(Select)支持搜索,Filament 会根据关联数据的标题字段返回搜索结果。如果你希望跨多列进行搜索,可以将包含列名的数组传递给 searchableOptions() 方法:
use Filament\Tables\Columns\SelectColumn;
SelectColumn::make('author_id')
->optionsRelationship(name: 'author', titleAttribute: 'name')
->searchableOptions(['name', 'email'])
预加载关联选项
如果你希望在页面加载时(而非用户进行搜索时)从数据库填充可搜索选项,可以使用 preloadOptions() 方法:
use Filament\Tables\Columns\SelectColumn;
SelectColumn::make('author_id')
->optionsRelationship(name: 'author', titleAttribute: 'name')
->searchableOptions()
->preloadOptions()
Optionally, you may pass a boolean value to control if the input should be preloaded or not:
use Filament\Tables\Columns\SelectColumn;
SelectColumn::make('author_id')
->optionsRelationship(name: 'author', titleAttribute: 'name')
->searchableOptions()
->preloadOptions(FeatureFlag::active())
As well as allowing a static value, the preloadOptions() method also accepts a function to dynamically calculate it. You can inject various utilities into the function as parameters.
了解更多 utility 注入详情。 | Utility | 类型 | 参数 | 描述 |
|---|---|---|---|
| Column | Filament\Tables\Columns\Column | $column | The current column instance. |
| Livewire | Livewire\Component | $livewire | The Livewire component instance. |
| Eloquent record | ?Illuminate\Database\Eloquent\Model | $record | The Eloquent record for the current table row. |
| Row loop | stdClass | $rowLoop | The row loop object for the current table row. |
| State | mixed | $state | The current value of the column, based on the current table row. |
| Table | Filament\Tables\Table | $table | The current table instance. |
排除当前记录
在处理递归关联时,你通常希望将当前记录从结果集中排除。
使用 ignoreRecord 参数可以轻松实现这一点:
use Filament\Tables\Columns\SelectColumn;
SelectColumn::make('parent_id')
->optionsRelationship(name: 'parent', titleAttribute: 'name', ignoreRecord: true)
自定义关联查询
你可以使用 optionsRelationship() 方法的第三个参数,来自定义用于检索选项的数据库查询:
use Filament\Tables\Columns\SelectColumn;
use Illuminate\Database\Eloquent\Builder;
SelectColumn::make('author_id')
->optionsRelationship(
name: 'author',
titleAttribute: 'name',
modifyQueryUsing: fn (Builder $query) => $query->withTrashed(),
)
The modifyQueryUsing argument can inject various utilities into the function as parameters.
了解更多 utility 注入详情。 | Utility | 类型 | 参数 | 描述 |
|---|---|---|---|
| Column | Filament\Tables\Columns\Column | $column | The current column instance. |
| Livewire | Livewire\Component | $livewire | The Livewire component instance. |
| Query | Illuminate\Database\Eloquent\Builder | $query | The Eloquent query builder to modify. |
| Eloquent record | ?Illuminate\Database\Eloquent\Model | $record | The Eloquent record for the current table row. |
| Row loop | stdClass | $rowLoop | The row loop object for the current table row. |
| Search | ?string | $search | The current search input value, if the field is searchable. |
| State | mixed | $state | The current value of the column, based on the current table row. |
| Table | Filament\Tables\Table | $table | The current table instance. |
自定义关联选项标签
如果你想自定义每个选项的标签,也许是为了更具描述性,或者连接名字和姓氏,你可以在数据库迁移中使用虚拟字段:
$table->string('full_name')->virtualAs('concat(first_name, \' \', last_name)');
use Filament\Tables\Columns\SelectColumn;
SelectColumn::make('author_id')
->optionsRelationship(name: 'author', titleAttribute: 'full_name')
或者,你可以使用 getOptionLabelFromRecordUsing() 方法将选项对应的 Eloquent 模型转换为标签:
use Filament\Tables\Columns\SelectColumn;
use Illuminate\Database\Eloquent\Builder;
use Illuminate\Database\Eloquent\Model;
SelectColumn::make('author_id')
->optionsRelationship(
name: 'author',
modifyQueryUsing: fn (Builder $query) => $query->orderBy('first_name')->orderBy('last_name'),
)
->getOptionLabelFromRecordUsing(fn (Model $record) => "{$record->first_name} {$record->last_name}")
->searchableOptions(['first_name', 'last_name'])
The getOptionLabelFromRecordUsing() method can inject various utilities into the function as parameters.
了解更多 utility 注入详情。 | Utility | 类型 | 参数 | 描述 |
|---|---|---|---|
| Column | Filament\Tables\Columns\Column | $column | The current column instance. |
| Livewire | Livewire\Component | $livewire | The Livewire component instance. |
| Eloquent record | Illuminate\Database\Eloquent\Model | $record | The Eloquent record to get the option label for. |
| Row loop | stdClass | $rowLoop | The row loop object for the current table row. |
| State | mixed | $state | The current value of the column, based on the current table row. |
| Table | Filament\Tables\Table | $table | The current table instance. |
Remembering options
By default, when using optionsRelationship(), Filament will remember the options for the duration of the table page to improve performance. This means that the options function will only run once per table page instead of once per cell. You can disable this behavior using the rememberOptions(false) method:
use Filament\Tables\Columns\SelectColumn;
SelectColumn::make('author_id')
->optionsRelationship(name: 'author', titleAttribute: 'name')
->rememberOptions(false)
NOTE
When options are remembered, any record-specific options or disabled options will not work correctly, as the same options will be used for all records in the table. If you need record-specific options or disabled options, you should disable option remembering.
As well as allowing a static value, the rememberOptions() method also accepts a function to dynamically calculate it. You can inject various utilities into the function as parameters.
了解更多 utility 注入详情。 | Utility | 类型 | 参数 | 描述 |
|---|---|---|---|
| Column | Filament\Tables\Columns\Column | $column | The current column instance. |
| Livewire | Livewire\Component | $livewire | The Livewire component instance. |
| Eloquent record | ?Illuminate\Database\Eloquent\Model | $record | The Eloquent record for the current table row. |
| Row loop | stdClass | $rowLoop | The row loop object for the current table row. |
| State | mixed | $state | The current value of the column, based on the current table row. |
| Table | Filament\Tables\Table | $table | The current table instance. |
允许在选项标签中使用 HTML
默认情况下,Filament 会对选项标签中的 HTML 进行转义。如果你希望允许使用 HTML,可以使用 allowOptionsHtml() 方法:
use Filament\Tables\Columns\SelectColumn;
SelectColumn::make('technology')
->options([
'tailwind' => '<span class="text-blue-500">Tailwind</span>',
'alpine' => '<span class="text-green-500">Alpine</span>',
'laravel' => '<span class="text-red-500">Laravel</span>',
'livewire' => '<span class="text-pink-500">Livewire</span>',
])
->searchableOptions()
->allowOptionsHtml()
NOTE
Be aware that you will need to ensure that the HTML is safe to render, otherwise your application will be vulnerable to XSS attacks.
或者,你可以传递一个布尔值来控制输入是否允许 HTML:
use Filament\Tables\Columns\SelectColumn;
SelectColumn::make('technology')
->options([
'tailwind' => '<span class="text-blue-500">Tailwind</span>',
'alpine' => '<span class="text-green-500">Alpine</span>',
'laravel' => '<span class="text-red-500">Laravel</span>',
'livewire' => '<span class="text-pink-500">Livewire</span>',
])
->searchableOptions()
->allowOptionsHtml(FeatureFlag::active())
As well as allowing a static value, the allowOptionsHtml() method also accepts a function to dynamically calculate it. You can inject various utilities into the function as parameters.
了解更多 utility 注入详情。 | Utility | 类型 | 参数 | 描述 |
|---|---|---|---|
| Column | Filament\Tables\Columns\Column | $column | The current column instance. |
| Livewire | Livewire\Component | $livewire | The Livewire component instance. |
| Eloquent record | ?Illuminate\Database\Eloquent\Model | $record | The Eloquent record for the current table row. |
| Row loop | stdClass | $rowLoop | The row loop object for the current table row. |
| State | mixed | $state | The current value of the column, based on the current table row. |
| Table | Filament\Tables\Table | $table | The current table instance. |
选项标签换行或截断
使用 JavaScript Select 组件时,超出 Select 元素宽度的标签默认会换行显示。你也可以选择截断溢出的标签。
use Filament\Tables\Columns\SelectColumn;
SelectColumn::make('truncate')
->wrapOptionLabels(false)
As well as allowing a static value, the wrapOptionLabels() method also accepts a function to dynamically calculate it. You can inject various utilities into the function as parameters.
了解更多 utility 注入详情。 | Utility | 类型 | 参数 | 描述 |
|---|---|---|---|
| Column | Filament\Tables\Columns\Column | $column | The current column instance. |
| Livewire | Livewire\Component | $livewire | The Livewire component instance. |
| Eloquent record | ?Illuminate\Database\Eloquent\Model | $record | The Eloquent record for the current table row. |
| Row loop | stdClass | $rowLoop | The row loop object for the current table row. |
| State | mixed | $state | The current value of the column, based on the current table row. |
| Table | Filament\Tables\Table | $table | The current table instance. |
禁用占位符选中
你可以使用 selectablePlaceholder(false) 方法禁止选中占位符(空选项):
use Filament\Tables\Columns\SelectColumn;
SelectColumn::make('status')
->options([
'draft' => 'Draft',
'reviewing' => 'Reviewing',
'published' => 'Published',
])
->selectablePlaceholder(false)
As well as allowing a static value, the selectablePlaceholder() method also accepts a function to dynamically calculate it. You can inject various utilities into the function as parameters.
了解更多 utility 注入详情。 | Utility | 类型 | 参数 | 描述 |
|---|---|---|---|
| Column | Filament\Tables\Columns\Column | $column | The current column instance. |
| Livewire | Livewire\Component | $livewire | The Livewire component instance. |
| Eloquent record | ?Illuminate\Database\Eloquent\Model | $record | The Eloquent record for the current table row. |
| Row loop | stdClass | $rowLoop | The row loop object for the current table row. |
| State | mixed | $state | The current value of the column, based on the current table row. |
| Table | Filament\Tables\Table | $table | The current table instance. |
禁用特定选项
你 disableOptionWhen() 方法禁用特定选项。该方法接收一个闭包,你可以在其中判断具有特定 $value 的选项是否应被禁用:
use Filament\Tables\Columns\SelectColumn;
SelectColumn::make('status')
->options([
'draft' => 'Draft',
'reviewing' => 'Reviewing',
'published' => 'Published',
])
->default('draft')
->disableOptionWhen(fn (string $value): bool => $value === 'published')
You can inject various utilities into the function as parameters.
了解更多 utility 注入详情。| Utility | 类型 | 参数 | 描述 |
|---|---|---|---|
| Column | Filament\Tables\Columns\Column | $column | The current column instance. |
| Option label | string | Illuminate\Contracts\Support\Htmlable | $label | The label of the option to disable. |
| Livewire | Livewire\Component | $livewire | The Livewire component instance. |
| Eloquent record | ?Illuminate\Database\Eloquent\Model | $record | The Eloquent record for the current table row. |
| Row loop | stdClass | $rowLoop | The row loop object for the current table row. |
| State | mixed | $state | The current value of the column, based on the current table row. |
| Table | Filament\Tables\Table | $table | The current table instance. |
| Option value | mixed | $value | The value of the option to disable. |
限制选项数量
你可以使用 optionsLimit() 方法来限制可搜索下拉选择框或多选框中显示的选项数量。默认值为 50:
use Filament\Tables\Columns\SelectColumn;
SelectColumn::make('author_id')
->optionsRelationship(name: 'author', titleAttribute: 'name')
->searchableOptions()
->optionsLimit(20)
请确保不要将限制值设置得过高,否则可能会因浏览器内存占用过高而导致选择控件变慢或无响应。
As well as allowing a static value, the optionsLimit() method also accepts a function to dynamically calculate it. You can inject various utilities into the function as parameters.
了解更多 utility 注入详情。 | Utility | 类型 | 参数 | 描述 |
|---|---|---|---|
| Column | Filament\Tables\Columns\Column | $column | The current column instance. |
| Livewire | Livewire\Component | $livewire | The Livewire component instance. |
| Eloquent record | ?Illuminate\Database\Eloquent\Model | $record | The Eloquent record for the current table row. |
| Row loop | stdClass | $rowLoop | The row loop object for the current table row. |
| State | mixed | $state | The current value of the column, based on the current table row. |
| Table | Filament\Tables\Table | $table | The current table instance. |
验证
你可以通过以数组方式传入任意 Laravel 验证规则来验证输入:
use Filament\Tables\Columns\SelectColumn;
SelectColumn::make('status')
->options([
'draft' => 'Draft',
'reviewing' => 'Reviewing',
'published' => 'Published',
])
->rules(['required'])
有效选项验证(in 规则)
in 规则确保用户无法选择不在选项列表中的值。这对保证数据完整性至关重要,因此 Filament 默认将此规则应用于所有 Select 字段。
由于 Select 字段填充选项的方式多种多样,且许多情况下选项并非默认全部加载(往往需要通过搜索来获取),Filament 会通过检查是否存在有效的“选项标签”来判断所选值是否有效。同时,它还会检查该选项是否处于禁用状态。
如果你使用自定义搜索查询来获取选项,请务必定义 getOptionLabelUsing() 方法,以便 Filament 能够根据可用选项验证所选值:
use Filament\Tables\Columns\SelectColumn;
SelectColumn::make('author_id')
->searchableOptions()
->getOptionsSearchResultsUsing(fn (string $search): array => Author::query()
->where('name', 'like', "%{$search}%")
->limit(50)
->pluck('name', 'id')
->all())
->getOptionLabelUsing(fn (string $value): ?string => Author::find($value)?->name),
如果选项无效,getOptionLabelUsing() 方法应返回 null,以便 Filament 能够判断所选值不在选项列表中;如果选项有效,则应返回该选项的标签。
如果你使用了 optionsRelationship() 方法,系统会自动为你定义 getOptionLabelUsing() 方法,因此你无需为此操心。
生命周期钩子
你可以使用钩子在 Select 组件生命周期的不同阶段执行代码:
SelectColumn::make()
->beforeStateUpdated(function ($record, $state) {
// Runs before the state is saved to the database.
})
->afterStateUpdated(function ($record, $state) {
// Runs after the state is saved to the database.
})
安全性
授权
Select(下拉选择)列在保存更改前,不会自动检查 Laravel 模型策略。当用户通过 Select 列更新值时,Filament 仅会检查该列是否处于 disabled()(禁用)状态,而不会执行任何 update 策略检查。这意味着,只要用户能在表格中看到某条记录且该列未被禁用,他们就能更新该列的值,无论你定义了何种 update 策略。如果你需要限制谁能编辑此列,应使用 disabled() 方法,根据自定义的授权逻辑有条件地禁止编辑;例如:disabled(fn ($record) => $record->user_id !== auth()->id())。或者,也可以考虑使用完整的编辑页面或模态框操作,在这些场景下 Filament 的资源授权机制会生效。
Still need help? Join our Discord community or open a GitHub discussion