Text column
简介
文本列用于展示普通文本:
use Filament\Tables\Columns\TextColumn;
TextColumn::make('title')

自定义颜色
你可以为文本设置颜色:
use Filament\Tables\Columns\TextColumn;
TextColumn::make('status')
->color('primary')
除了允许静态值之外,color() 方法也可以接受函数来动态计算其值。你可以将多个 utility 作为参数注入到函数中。
Learn more about utility injection.| Utility | Type | Parameter | Description |
|---|---|---|---|
| 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 <a href="https://laravel.com/docs/blade#the-loop-variable" target="_blank">row loop</a> 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. |

添加图标
文本列也可以添加图标:
use Filament\Tables\Columns\TextColumn;
use Filament\Support\Icons\Heroicon;
TextColumn::make('email')
->icon(Heroicon::Envelope)
icon() 方法也可以接受函数来图标计算其图标。你可以将多个 utility 作为参数注入到函数中。
Learn more about utility injection.| Utility | Type | Parameter | Description |
|---|---|---|---|
| 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 <a href="https://laravel.com/docs/blade#the-loop-variable" target="_blank">row loop</a> 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. |
你可以使用 iconPosition() 方法设置图标的位置:
use Filament\Tables\Columns\TextColumn;
use Filament\Support\Enums\IconPosition;
use Filament\Support\Icons\Heroicon;
TextColumn::make('email')
->icon(Heroicon::Envelope)
->iconPosition(IconPosition::After) // `IconPosition::Before` or `IconPosition::After`
iconPosition() 方法也可以接受函数来图标计算图标位置。你可以将多个 utility 作为参数注入到函数中。
Learn more about utility injection.| Utility | Type | Parameter | Description |
|---|---|---|---|
| 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 <a href="https://laravel.com/docs/blade#the-loop-variable" target="_blank">row loop</a> 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. |
图标颜色默认为文本颜色,不过你可以使用 iconColor() 方法,单独自定义图标颜色:
use Filament\Tables\Columns\TextColumn;
use Filament\Support\Icons\Heroicon;
TextColumn::make('email')
->icon(Heroicon::Envelope)
->iconColor('primary')
iconColor() 方法也可以接受函数来图标计算图标颜色。你可以将多个 utility 作为参数注入到函数中。.
Learn more about utility injection.| Utility | Type | Parameter | Description |
|---|---|---|---|
| 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 <a href="https://laravel.com/docs/blade#the-loop-variable" target="_blank">row loop</a> 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. |
显示为"徽章"
默认情况下,文本非常简单,没有背景色。你可以使用 badge() 方法将其显示为"徽章"。这方面的一个很好的用例是状态展示,其中可能需要显示与状态相匹配的颜色徽章:
use Filament\Tables\Columns\TextColumn;
TextColumn::make('status')
->badge()
->color(fn (string $state): string => match ($state) {
'draft' => 'gray',
'reviewing' => 'warning',
'published' => 'success',
'rejected' => 'danger',
})

你可以添加其他东西诸如图标到徽章中。
或者,你也可以传入布尔值以控制文本是否展示为徽章:
use Filament\Tables\Columns\TextColumn;
TextColumn::make('status')
->badge(FeatureFlag::active())
除了允许静态值之外,badge() 方法也可以接受函数来动态计算其值。你可以将多个 utility 作为参数注入到函数中。
Learn more about utility injection.| Utility | Type | Parameter | Description |
|---|---|---|---|
| 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 <a href="https://laravel.com/docs/blade#the-loop-variable" target="_blank">row loop</a> 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. |
格式化
当使用文本列字段时,你可能希望在 UI 中实际输出不同于该字段原始状态值的文本,原始值通常自动从 Eloquent 模型中检索而来。格式化状态在保留原始数据的完整性的同时,允许你将其以对用户更友好的方式展示。
要在不改变原始状态值的同时格式化文本列的值,你可以使用 formatStateUsing() 方法。该方法允许你接受一个函数,该函数接受状态值作为参数并返回格式化后的值:
use Filament\Tables\Columns\TextColumn;
TextColumn::make('status')
->formatStateUsing(fn (string $state): string => __("statuses.{$state}"))
上例中,数据库的 status 字段可能包含 draft、reviewing、published 或者 rejected 等值,而格式化后的状态值为这些值的翻译后的版本。
传入 formatStateUsing() 的函数可以将多个 utility 作为参数注入到该函数中。
Learn more about utility injection.| Utility | Type | Parameter | Description |
|---|---|---|---|
| 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 <a href="https://laravel.com/docs/blade#the-loop-variable" target="_blank">row loop</a> 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. |
日期格式化
除了传入函数到 formatStateUsing() 之外,你也可以使用 date()、dateTime() 和 time() 方法,使用 PHP 日期格式化 token 对列状态进行格式化:
use Filament\Tables\Columns\TextColumn;
TextColumn::make('created_at')
->date()
TextColumn::make('created_at')
->dateTime()
TextColumn::make('created_at')
->time()
你也可以传入自定义格式化字符串到 date()、dateTime() 或者 time() 方法,自定义日期格式。你可以使用 PHP 日期格式化 token作为格式化字符串值:
use Filament\Tables\Columns\TextColumn;
TextColumn::make('created_at')
->date('M j, Y')
TextColumn::make('created_at')
->dateTime('M j, Y H:i:s')
TextColumn::make('created_at')
->time('H:i:s')
除了允许静态值之外,date()、dateTime() 和 time() 方法也可以接受函数来动态计算格式。你可以将多个 utility 作为参数注入到这些函数中。
Learn more about utility injection.| Utility | Type | Parameter | Description |
|---|---|---|---|
| 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 <a href="https://laravel.com/docs/blade#the-loop-variable" target="_blank">row loop</a> 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. |
使用 Carbon 的 macro 格式格式化日期
你也可以使用 isoDate()、isoDateTime() 和 isoTime() 方法,使用 Carbon 的 macro 格式来格式化列状态值:
use Filament\Tables\Columns\TextColumn;
TextColumn::make('created_at')
->isoDate()
TextColumn::make('created_at')
->isoDateTime()
TextColumn::make('created_at')
->isoTime()
通过将自定义宏格式化字符串传入到 isoDate()、isoDateTime() 或者 isoTime() 方法,你可以自定义日期格式化。你可以使用 Carbon 的宏格式:
use Filament\Tables\Columns\TextColumn;
TextColumn::make('created_at')
->isoDate('L')
TextColumn::make('created_at')
->isoDateTime('LLL')
TextColumn::make('created_at')
->isoTime('LT')
除了允许静态值之外,isoDate()、isoDateTime() 和 isoTime() 方法也可以接受函数来动态计算格式。你可以将多个 utility 作为参数注入到这些函数中。
Learn more about utility injection.| Utility | Type | Parameter | Description |
|---|---|---|---|
| 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 <a href="https://laravel.com/docs/blade#the-loop-variable" target="_blank">row loop</a> 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. |


