信息列表
概述
简介
 
  Entry 类可以在 Filament\Infolists\Components 命名空间中找到。它们位于组件的 Schema 数组中。Filament 包含许多内置的 Entry:
你也可以创建自定义 Entry,以你希望的形式显示数据。
条目(Entry)可以使用静态 make() 方法创建,传入其唯一名称。通常,Entry 的名称对应于 Eloquent 模型的属性名。你可以使用“点语法”访问关联中的属性:
use Filament\Infolists\Components\TextEntry;
TextEntry::make('title')
TextEntry::make('author.name')
 
  Entry 内容 (state)
Entry 旨在提供一种简单易用、优化展示 Eloquent 记录数据的方式。尽管如此,它很灵活,你可以展示任何来源的数据,不只是 Eloquent 记录的属性。
Entry 展示的数据称为“状态”。当使用面板资源时,信息列表了解它们要展示的记录。也就是说,Entry 的状态是基于记录的属性值设置的。比如,如果 Entry 用在 PostResource 的信息列表中,那么当前贴文(post)的 title 属性值将会被显示。
use Filament\Infolists\Components\TextEntry;
TextEntry::make('title')
如果你想访问存储在关联中的值,你可以使用“点语法”。首先是你要访问数据的关联名,随后紧跟着点号,然后是属性名:
use Filament\Infolists\Components\TextEntry;
TextEntry::make('author.name')
你也可以使用“点语法”访问 Eloquent 模型中的 JSON/数组字段的值。首先是属性名,紧随着点号,最后是你想要读取的 JSON 对象的键名:
use Filament\Infolists\Components\TextEntry;
TextEntry::make('meta.title')
设置 Entry 状态
使用 state() 方法,你可以将你自己的状态传入到 Entry 中:
use Filament\Infolists\Components\TextEntry;
TextEntry::make('title')
    ->state('Hello, world!')
 state() 方法也可以接受函数来动态计算其状态。你可以将各种 utility 作为参数注入到函数中。 
 
Learn more about utility injection.
 | Utility | Type | Parameter | Description | 
|---|---|---|---|
| Entry | Filament\Infolists\Components\Entry | $component | The current entry component instance. | 
| Get function | Filament\Schemas\Components\Utilities\Get | $get | A function for retrieving values from the current schema data. Validation is not run on form fields. | 
| Livewire | Livewire\Component | $livewire | The Livewire component instance. | 
| Eloquent model FQN | ?string<Illuminate\Database\Eloquent\Model> | $model | The Eloquent model FQN for the current schema. | 
| Operation | string | $operation | The current operation being performed by the schema. Usually create,edit, orview. | 
| Eloquent record | ?Illuminate\Database\Eloquent\Model | $record | The Eloquent record for the current schema. | 
| State | mixed | $state | The current value of the entry. | 
设置 Entry 的默认状态
当 Entry 为空(其状态值为 null)时,你可以使用 default() 方法定义要使用的备用状态值。该方法将会视默认状态为真实状态,使得像图片或者颜色这些 Entry 可以展示默认图片或者颜色。
use Filament\Infolists\Components\TextEntry;
TextEntry::make('title')
    ->default('Untitled')
Entry 为空时添加占位符文本
有时,你想在 Entry 为空状态值式显示占位符文本,其使用青灰色文本。这与默认值不同,因为占位符永远是文本,且不会被当作真实状态。
use Filament\Infolists\Components\TextEntry;
TextEntry::make('title')
    ->placeholder('Untitled')
 
  设置 Entry 的标签
默认情况下,Entry 的标签(展示在信息列表头部),是由 Entry 的名称生成。你可以使用 label() 方法对其自定义:
use Filament\Infolists\Components\TextEntry;
TextEntry::make('name')
    ->label('Full name')
 除了允许静态值之外,label() 方法也可以接受函数来动态计算其值。你可以将各种 utility 作为参数注入到函数中。 
 
Learn more about utility injection.
 | Utility | Type | Parameter | Description | 
|---|---|---|---|
| Entry | Filament\Infolists\Components\Entry | $component | The current entry component instance. | 
| Get function | Filament\Schemas\Components\Utilities\Get | $get | A function for retrieving values from the current schema data. Validation is not run on form fields. | 
| Livewire | Livewire\Component | $livewire | The Livewire component instance. | 
| Eloquent model FQN | ?string<Illuminate\Database\Eloquent\Model> | $model | The Eloquent model FQN for the current schema. | 
| Operation | string | $operation | The current operation being performed by the schema. Usually create,edit, orview. | 
| Eloquent record | ?Illuminate\Database\Eloquent\Model | $record | The Eloquent record for the current schema. | 
| State | mixed | $state | The current value of the entry. | 
如果你想本地化翻译字符,以此方式自定义标签很有用:
use Filament\Infolists\Components\TextEntry;
TextEntry::make('name')
    ->label(__('entries.name'))
隐藏 Entry 标签
TIP
如果你想要隐藏条目(Entry)的标签,可能是因为你尝试将条目用于任意文本或 UI。条目专门设计用于以结构化方式显示数据,而 Prime 组件 是用于渲染基本独立静态内容(例如文本、图像和按钮(操作))的简单组件。你可以考虑使用 Prime 组件。
将标签设置为空字符串来隐藏它可能很诱人,但不建议这样做。即使标签的用途在视觉上很清晰,将其设置为空字符串也无法向屏幕阅读器传达条目的用途。你应该使用 hiddenLabel() 方法,这样虽然在视觉上隐藏了标签,但屏幕阅读器仍然可以访问它:
use Filament\Infolists\Components\TextEntry;
TextEntry::make('name')
    ->hiddenLabel()
或者,你也可以传入布尔值以控制是否隐藏标签:
use Filament\Infolists\Components\TextEntry;
TextEntry::make('name')
    ->hiddenLabel(FeatureFlag::active())
 除了允许静态值之外,hiddenLabel() 方法也可以接受函数来动态计算其值。你可以将各种 utility 作为参数注入到函数中。 
 
Learn more about utility injection.
 | Utility | Type | Parameter | Description | 
|---|---|---|---|
| Entry | Filament\Infolists\Components\Entry | $component | The current entry component instance. | 
| Get function | Filament\Schemas\Components\Utilities\Get | $get | A function for retrieving values from the current schema data. Validation is not run on form fields. | 
| Livewire | Livewire\Component | $livewire | The Livewire component instance. | 
| Eloquent model FQN | ?string<Illuminate\Database\Eloquent\Model> | $model | The Eloquent model FQN for the current schema. | 
| Operation | string | $operation | The current operation being performed by the schema. Usually create,edit, orview. | 
| Eloquent record | ?Illuminate\Database\Eloquent\Model | $record | The Eloquent record for the current schema. | 
| State | mixed | $state | The current value of the entry. | 
点击 Entry 时打开 URL
使用 url() 方法,并传入一个 URL,你可以点击 Entry 来打开 URL。
use Filament\Infolists\Components\TextEntry;
TextEntry::make('title')
    ->url('/about/titles')
你也可以传入一个函数到 url() 方法中,来动态计算 URL。比如,通过将 $record 作为参数注入,你可以访问对应信息列表的当前 Eloquent 记录:
use Filament\Infolists\Components\TextEntry;
TextEntry::make('title')
    ->url(fn (Post $record): string => route('posts.edit', ['post' => $record]))
如果你使用了面板资源,你可以使用 getUrl() 方法为该记录生成到页面的链接:
use App\Filament\Posts\PostResource;
use Filament\Infolists\Components\TextEntry;
TextEntry::make('title')
    ->url(fn (Post $record): string => PostResource::getUrl('edit', ['record' => $record]))
 传递给 url() 的函数可以注入各种 utility 作为参数。 
 
Learn more about utility injection.
 | Utility | Type | Parameter | Description | 
|---|---|---|---|
| Entry | Filament\Infolists\Components\Entry | $component | The current entry component instance. | 
| Get function | Filament\Schemas\Components\Utilities\Get | $get | A function for retrieving values from the current schema data. Validation is not run on form fields. | 
| Livewire | Livewire\Component | $livewire | The Livewire component instance. | 
| Eloquent model FQN | ?string<Illuminate\Database\Eloquent\Model> | $model | The Eloquent model FQN for the current schema. | 
| Operation | string | $operation | The current operation being performed by the schema. Usually create,edit, orview. | 
| Eloquent record | ?Illuminate\Database\Eloquent\Model | $record | The Eloquent record for the current schema. | 
| State | mixed | $state | The current value of the entry. | 
你也可以选择在新标签页中打开 URL:
use Filament\Infolists\Components\TextEntry;
TextEntry::make('title')
    ->url(fn (Post $record): string => PostResource::getUrl('edit', ['record' => $record]))
    ->openUrlInNewTab()
或者,你也可以传入一个布尔值,用以控制 URL 是否应该在新标签页中打开:
use Filament\Infolists\Components\TextEntry;
TextEntry::make('title')
    ->url(fn (Post $record): string => PostResource::getUrl('edit', ['record' => $record]))
    ->openUrlInNewTab(FeatureFlag::active())
 除了允许静态值之外,openUrlInNewTab() 方法也可以接受函数来动态计算其值。你可以将各种 utility 作为参数注入到函数中。 
 
Learn more about utility injection.
 | Utility | Type | Parameter | Description | 
|---|---|---|---|
| Entry | Filament\Infolists\Components\Entry | $component | The current entry component instance. | 
| Get function | Filament\Schemas\Components\Utilities\Get | $get | A function for retrieving values from the current schema data. Validation is not run on form fields. | 
| Livewire | Livewire\Component | $livewire | The Livewire component instance. | 
| Eloquent model FQN | ?string<Illuminate\Database\Eloquent\Model> | $model | The Eloquent model FQN for the current schema. | 
| Operation | string | $operation | The current operation being performed by the schema. Usually create,edit, orview. | 
| Eloquent record | ?Illuminate\Database\Eloquent\Model | $record | The Eloquent record for the current schema. | 
| State | mixed | $state | The current value of the entry. | 
隐藏 Entry
你可以隐藏 Entry:
use Filament\Infolists\Components\TextEntry;
TextEntry::make('role')
    ->hidden()
或者,你也可以传入一个布尔值,以控制是否隐藏 Entry:
use Filament\Infolists\Components\TextEntry;
TextEntry::make('role')
    ->hidden(! FeatureFlag::active())
 除了允许静态值之外,hidden() 方法也可以接受函数来动态计算其值。你可以将各种 utility 作为参数注入到函数中。 
 
Learn more about utility injection.
 | Utility | Type | Parameter | Description | 
|---|---|---|---|
| Entry | Filament\Infolists\Components\Entry | $component | The current entry component instance. | 
| Get function | Filament\Schemas\Components\Utilities\Get | $get | A function for retrieving values from the current schema data. Validation is not run on form fields. | 
| Livewire | Livewire\Component | $livewire | The Livewire component instance. | 
| Eloquent model FQN | ?string<Illuminate\Database\Eloquent\Model> | $model | The Eloquent model FQN for the current schema. | 
| Operation | string | $operation | The current operation being performed by the schema. Usually create,edit, orview. | 
| Eloquent record | ?Illuminate\Database\Eloquent\Model | $record | The Eloquent record for the current schema. | 
| State | mixed | $state | The current value of the entry. | 
此外,你也可以使用 visible() 方法来控制是否隐藏条目。在某些情况下,此方法可以让你的代码更具可读性:
use Filament\Infolists\Components\TextEntry;
TextEntry::make('role')
    ->visible(FeatureFlag::active())
 除了允许静态值之外,visible() 方法也可以接受函数来动态计算其值。你可以将各种 utility 作为参数注入到函数中。 
 
Learn more about utility injection.
 | Utility | Type | Parameter | Description | 
|---|---|---|---|
| Entry | Filament\Infolists\Components\Entry | $component | The current entry component instance. | 
| Get function | Filament\Schemas\Components\Utilities\Get | $get | A function for retrieving values from the current schema data. Validation is not run on form fields. | 
| Livewire | Livewire\Component | $livewire | The Livewire component instance. | 
| Eloquent model FQN | ?string<Illuminate\Database\Eloquent\Model> | $model | The Eloquent model FQN for the current schema. | 
| Operation | string | $operation | The current operation being performed by the schema. Usually create,edit, orview. | 
| Eloquent record | ?Illuminate\Database\Eloquent\Model | $record | The Eloquent record for the current schema. | 
| State | mixed | $state | The current value of the entry. | 
NOTE
如果同时使用了 hidden() 和 visible() 方法,那么它们都需要告知该条目是否应该展示。
使用 JavaScript 隐藏 Entry
如果你需要根据用户交互隐藏条目,可以使用 hidden() 或者 visible() 方法,并传入一个使用 utility 注入的函数,以确定该 Entry 是否该隐藏:
use Filament\Forms\Components\Select;
use Filament\Infolists\Components\IconEntry;
Select::make('role')
    ->options([
        'user' => 'User',
        'staff' => 'Staff',
    ])
    ->live()
IconEntry::make('is_admin')
    ->boolean()
    ->hidden(fn (Get $get): bool => $get('role') !== 'staff')
本例中,role 字段设置成 live(),这意味着,该 role 字段每次发生变化时, Schema 都会重新加载。这将会导致传入 hidden() 方法的函数重新计算,使之在 role Entry 未设为 staff 时隐藏 is_admin 字段。
不过,每次都重新加载 Schema 会导致每次都需要重新发起网络请求,因为你没办法在客户端重新运行 PHP 函数。这对于性能不太友好。
作为替代方案,你可以编写 JavaScript 使之基于另一个字段值隐藏条目。这可以通过将 JavaScript 表达式传递给 hiddenJs() 方法来实现:
use Filament\Forms\Components\Select;
use Filament\Infolists\Components\IconEntry;
Select::make('role')
    ->options([
        'user' => 'User',
        'staff' => 'Staff',
    ])
IconEntry::make('is_admin')
    ->boolean()
    ->hiddenJs(<<<'JS'
        $get('role') !== 'staff'
        JS)
虽然,传递给 hiddenJs() 的代码非常类似于 PHP,但它其实是 JavaScript。Filament 为 JavaScript 提供了 $get() 实用函数,使之行为与 PHP 的等效函数非常相似,而无需依赖于 Entry 的 live()。
visibleJs() 方法的用法也类似于 hiddenJs(),它用以控制 Entry 是否为可见:
use Filament\Forms\Components\Select;
use Filament\Infolists\Components\IconEntry;
Select::make('role')
    ->options([
        'user' => 'User',
        'staff' => 'Staff',
    ])
    
IconEntry::make('is_admin')
    ->boolean()
    ->visibleJs(<<<'JS'
        $get('role') === 'staff'
        JS)
NOTE
如果同时使用了 hiddenJs() 和 visibleJs(),则需要它们都说明该 Entry 是否可见才能显示。
基于当前操作隐藏 Entry
Schema 的“操作(Operaton)”指的是在其上面执行的当前 Action。通常,如果你使用的是面板资源,则可以是 create、edit 或者 view。
你可以通过将操作传递给 hiddenOn() 方法来基于当前操作隐藏 Entry:
use Filament\Infolists\Components\IconEntry;
IconEntry::make('is_admin')
    ->boolean()
    ->hiddenOn('edit')
    
// is the same as
IconEntry::make('is_admin')
    ->boolean()
    ->hidden(fn (string $operation): bool => $operation === 'edit')
你可以将一个操作数组传递个 hiddenOn() 方法,如果当前操作是数组中的其中一项,该 Entry 将会被隐藏:
use Filament\Infolists\Components\IconEntry;
IconEntry::make('is_admin')
    ->boolean()
    ->hiddenOn(['edit', 'view'])
    
// is the same as
IconEntry::make('is_admin')
    ->boolean()
    ->hidden(fn (string $operation): bool => in_array($operation, ['edit', 'view']))
NOTE
hiddenOn() 方法将会覆盖任何之前的 hidden() 调用,反之亦然。
此外,你也可以使用 visibleOn() 控制是否隐藏 Entry。在某些情况下,该方法会让你的代码更具可读性:
use Filament\Infolists\Components\IconEntry;
IconEntry::make('is_admin')
    ->boolean()
    ->visibleOn('create')
IconEntry::make('is_admin')
    ->boolean()
    ->visibleOn(['create', 'edit'])
NOTE
VisibleOn() 方法将会覆盖任何之前的 visible() 调用,反之亦然。
行内标签
Entry 可以设置为让标签与之同行显示,而不是让标签在其上方。这对于有许多标签的信息列表非常有用,因为其纵向空间非常宝贵。要让 Entry 的标签在行内展示,请使用 inlineLabel() 方法:
use Filament\Infolists\Components\TextEntry;
TextInput::make('name')
    ->inlineLabel()
 
  此外,你也可以传入一个布尔值,控制其标签是否行内显示:
use Filament\Infolists\Components\TextInput;
TextInput::make('name')
    ->inlineLabel(FeatureFlag::active())
 除了允许静态值之外,inlineLabel() 方法也可以接受函数来动态计算其值。你可以将各种 utility 作为参数注入到函数中。 
 
Learn more about utility injection.
 | Utility | Type | Parameter | Description | 
|---|---|---|---|
| Entry | Filament\Infolists\Components\Entry | $component | The current entry component instance. | 
| Get function | Filament\Schemas\Components\Utilities\Get | $get | A function for retrieving values from the current schema data. Validation is not run on form fields. | 
| Livewire | Livewire\Component | $livewire | The Livewire component instance. | 
| Eloquent model FQN | ?string<Illuminate\Database\Eloquent\Model> | $model | The Eloquent model FQN for the current schema. | 
| Operation | string | $operation | The current operation being performed by the schema. Usually create,edit, orview. | 
| Eloquent record | ?Illuminate\Database\Eloquent\Model | $record | The Eloquent record for the current schema. | 
| State | mixed | $state | The current value of the entry. | 
一次性在多处使用行内标签
如果你希望在 布局组件(例如 section 或 tab)中以内联方式显示所有标签,则可以在组件本身上使用 inlineLabel(),其中所有条目的标签都将以内联方式显示:
use Filament\Infolists\Components\TextInput;
use Filament\Schemas\Components\Section;
Section::make('Details')
    ->inlineLabel()
    ->entries([
        TextInput::make('name'),
        TextInput::make('email')
            ->label('Email address'),
        TextInput::make('phone')
            ->label('Phone number'),
    ])
 
  你也可以在全体 Schema 上使用 inlineLabel() 来行内显示所有标签:
use Filament\Schemas\Schema;
public function infolist(Schema $schema): Schema
{
    return $schema
        ->inlineLabel()
        ->components([
            // ...
        ]);
}
在布局组件或 Schema 上使用 inlineLabel() 时,你仍然可以通过在条目上使用 inlineLabel(false) 方法选择退出单个条目的内联标签:
use Filament\Infolists\Components\TextInput;
use Filament\Schemas\Components\Section;
Section::make('Details')
    ->inlineLabel()
    ->entries([
        TextInput::make('name'),
        TextInput::make('email')
            ->label('Email address'),
        TextInput::make('phone')
            ->label('Phone number')
            ->inlineLabel(false),
    ])
为条目添加 Tooltip
你可以指定鼠标悬停在条目上时显示的 Tooltip:
use Filament\Infolists\Components\TextEntry;
TextEntry::make('title')
    ->tooltip('Shown at the top of the page')
 除了允许静态值之外,tooltip() 方法也可以接受函数来动态计算其值。你可以将各种 utility 作为参数注入到函数中。 
 
Learn more about utility injection.
 | Utility | Type | Parameter | Description | 
|---|---|---|---|
| Entry | Filament\Infolists\Components\Entry | $component | The current entry component instance. | 
| Get function | Filament\Schemas\Components\Utilities\Get | $get | A function for retrieving values from the current schema data. Validation is not run on form fields. | 
| Livewire | Livewire\Component | $livewire | The Livewire component instance. | 
| Eloquent model FQN | ?string<Illuminate\Database\Eloquent\Model> | $model | The Eloquent model FQN for the current schema. | 
| Operation | string | $operation | The current operation being performed by the schema. Usually create,edit, orview. | 
| Eloquent record | ?Illuminate\Database\Eloquent\Model | $record | The Eloquent record for the current schema. | 
| State | mixed | $state | The current value of the entry. | 
 
  对齐 Entry 内容
你可以使用 alignStart()、alignCenter() 或 alignEnd() 方法将条目的内容与开始(在从左到右的界面中为左侧,在从右到左的界面中为右侧)、中心或结束(在从左到右的界面中为右侧,在从右到左的界面中为左侧)对齐:
use Filament\Infolists\Components\TextEntry;
TextEntry::make('title')
    ->alignStart() // This is the default alignment.
TextEntry::make('title')
    ->alignCenter()
TextEntry::make('title')
    ->alignEnd()
或者,你可以将 Alignment 枚举传递给 alignment() 方法:
use Filament\Infolists\Components\TextEntry;
use Filament\Support\Enums\Alignment;
TextEntry::make('title')
    ->alignment(Alignment::Center)
 除了允许静态值之外,alignment() 方法也可以接受函数来动态计算其值。你可以将各种 utility 作为参数注入到函数中。 
 
Learn more about utility injection.
 | Utility | Type | Parameter | Description | 
|---|---|---|---|
| Entry | Filament\Infolists\Components\Entry | $component | The current entry component instance. | 
| Get function | Filament\Schemas\Components\Utilities\Get | $get | A function for retrieving values from the current schema data. Validation is not run on form fields. | 
| Livewire | Livewire\Component | $livewire | The Livewire component instance. | 
| Eloquent model FQN | ?string<Illuminate\Database\Eloquent\Model> | $model | The Eloquent model FQN for the current schema. | 
| Operation | string | $operation | The current operation being performed by the schema. Usually create,edit, orview. | 
| Eloquent record | ?Illuminate\Database\Eloquent\Model | $record | The Eloquent record for the current schema. | 
| State | mixed | $state | The current value of the entry. | 
添加额外内容到 Entry 中
条目包含许多“插槽”,可在子 Schema 中插入内容。插槽可以接受文本、任何 Schema 组件、操作 和 操作组。通常,Prime 组件 用于提供内容。
以下插槽适用于所有条目:
- aboveLabel()
- beforeLabel()
- afterLabel()
- belowLabel()
- aboveContent()
- beforeContent()
- afterContent()
- belowContent()
除了允许静态值之外,这些插槽方法也允许接受函数来动态计算它们的值。你可以注入各种 utility 到函数中作为参数。
Learn more about utility injection.| Utility | Type | Parameter | Description | 
|---|---|---|---|
| Entry | Filament\Infolists\Components\Entry | $component | The current entry component instance. | 
| Get function | Filament\Schemas\Components\Utilities\Get | $get | A function for retrieving values from the current schema data. Validation is not run on form fields. | 
| Livewire | Livewire\Component | $livewire | The Livewire component instance. | 
| Eloquent model FQN | ?string<Illuminate\Database\Eloquent\Model> | $model | The Eloquent model FQN for the current schema. | 
| Operation | string | $operation | The current operation being performed by the schema. Usually create,edit, orview. | 
| Eloquent record | ?Illuminate\Database\Eloquent\Model | $record | The Eloquent record for the current schema. | 
| State | mixed | $state | The current value of the entry. | 
要插入普通文本,你可以传入字符串到这些方法中:
use Filament\Infolists\Components\TextEntry;
TextEntry::make('name')
    ->belowContent('This is the user\'s full name.')
 
  To insert a schema component, often a prime component, you can pass the component to the method:
use Filament\Infolists\Components\TextEntry;
use Filament\Schemas\Components\Text;
use Filament\Support\Enums\FontWeight;
TextEntry::make('name')
    ->belowContent(Text::make('This is the user\'s full name.')->weight(FontWeight::Bold))
 
  To insert an action or action group, you can pass the action or action group to the method:
use Filament\Actions\Action;
use Filament\Infolists\Components\TextEntry;
TextEntry::make('name')
    ->belowContent(Action::make('generate'))
 
  你可以通过将内容数组传递给方法,将任意内容组合插入插槽:
use Filament\Actions\Action;
use Filament\Infolists\Components\TextEntry;
use Filament\Schemas\Components\Icon;
use Filament\Support\Icons\Heroicon;
TextEntry::make('name')
    ->belowContent([
        Icon::make(Heroicon::InformationCircle),
        'This is the user\'s full name.',
        Action::make('generate'),
    ])
 
  你可以通过将内容数组传递给 Schema::start()(默认)、Schema::end() 或 Schema::between() 来对齐插槽中的内容:
use Filament\Actions\Action;
use Filament\Infolists\Components\TextEntry;
use Filament\Schemas\Components\Flex;
use Filament\Schemas\Components\Icon;
use Filament\Schemas\Schema;
use Filament\Support\Icons\Heroicon;
TextEntry::make('name')
    ->belowContent(Schema::end([
        Icon::make(Heroicon::InformationCircle),
        'This is the user\'s full name.',
        Action::make('generate'),
    ]))
TextEntry::make('name')
    ->belowContent(Schema::between([
        Icon::make(Heroicon::InformationCircle),
        'This is the user\'s full name.',
        Action::make('generate'),
    ]))
TextEntry::make('name')
    ->belowContent(Schema::between([
        Flex::make([
            Icon::make(Heroicon::InformationCircle)
                ->grow(false),
            'This is the user\'s full name.',
        ]),
        Action::make('generate'),
    ]))
TIP
正如你在上面的 Schema::between() 示例中所见,我们使用了一个 Flex 组件 将图标和文本组合在一起,使它们之间没有空白。图标使用 grow(false) 来防止其占用一半的水平空间,从而允许文本占用剩余的空间。
 
  在 Entry 标签上方添加额外内容
使用 aboveLabel() 方法,你可以在 Entry 标签上方添加额外内容。你可以传入任何内容到该方法,包括文本、Schema 组件、Action 或者 Action 分组:
use Filament\Infolists\Components\TextEntry;
use Filament\Schemas\Components\Icon;
use Filament\Support\Icons\Heroicon;
TextEntry::make('name')
    ->aboveLabel([
        Icon::make(Heroicon::Star),
        'This is the content above the entry\'s label'
    ])
 除了允许静态值之外,aboveLabel() 方法也可以接受函数来动态计算其值。你可以将各种 utility 作为参数注入到函数中。 
 
Learn more about utility injection.
 | Utility | Type | Parameter | Description | 
|---|---|---|---|
| Entry | Filament\Infolists\Components\Entry | $component | The current entry component instance. | 
| Get function | Filament\Schemas\Components\Utilities\Get | $get | A function for retrieving values from the current schema data. Validation is not run on form fields. | 
| Livewire | Livewire\Component | $livewire | The Livewire component instance. | 
| Eloquent model FQN | ?string<Illuminate\Database\Eloquent\Model> | $model | The Eloquent model FQN for the current schema. | 
| Operation | string | $operation | The current operation being performed by the schema. Usually create,edit, orview. | 
| Eloquent record | ?Illuminate\Database\Eloquent\Model | $record | The Eloquent record for the current schema. | 
| State | mixed | $state | The current value of the entry. | 
 
  在 Entry 标签之前添加额外内容
使用 beforeLabel() 方法,你可以在 Entry 标签之前添加额外内容。你可以传入任何内容到该方法,包括文本、Schema 组件、Action 或者 Action 分组:
use Filament\Infolists\Components\TextEntry;
use Filament\Schemas\Components\Icon;
use Filament\Support\Icons\Heroicon;
TextEntry::make('name')
    ->beforeLabel(Icon::make(Heroicon::Star))
 除了允许静态值之外,beforeLabel() 方法也可以接受函数来动态计算其值。你可以将各种 utility 作为参数注入到函数中。 
 
Learn more about utility injection.
 | Utility | Type | Parameter | Description | 
|---|---|---|---|
| Entry | Filament\Infolists\Components\Entry | $component | The current entry component instance. | 
| Get function | Filament\Schemas\Components\Utilities\Get | $get | A function for retrieving values from the current schema data. Validation is not run on form fields. | 
| Livewire | Livewire\Component | $livewire | The Livewire component instance. | 
| Eloquent model FQN | ?string<Illuminate\Database\Eloquent\Model> | $model | The Eloquent model FQN for the current schema. | 
| Operation | string | $operation | The current operation being performed by the schema. Usually create,edit, orview. | 
| Eloquent record | ?Illuminate\Database\Eloquent\Model | $record | The Eloquent record for the current schema. | 
| State | mixed | $state | The current value of the entry. | 
 
  在 Entry 标签之后添加额外内容
使用 afterLabel() 方法,你可以在 Entry 标签之后添加额外内容。你可以传入任何内容到该方法,包括文本、Schema 组件、Action 或者 Action 分组:
use Filament\Infolists\Components\TextEntry;
use Filament\Schemas\Components\Icon;
use Filament\Support\Icons\Heroicon;
TextEntry::make('name')
    ->afterLabel([
        Icon::make(Heroicon::Star),
        'This is the content after the entry\'s label'
    ])
 除了允许静态值之外,afterLabel() 方法也可以接受函数来动态计算其值。你可以将各种 utility 作为参数注入到函数中。 
 
Learn more about utility injection.
 | Utility | Type | Parameter | Description | 
|---|---|---|---|
| Entry | Filament\Infolists\Components\Entry | $component | The current entry component instance. | 
| Get function | Filament\Schemas\Components\Utilities\Get | $get | A function for retrieving values from the current schema data. Validation is not run on form fields. | 
| Livewire | Livewire\Component | $livewire | The Livewire component instance. | 
| Eloquent model FQN | ?string<Illuminate\Database\Eloquent\Model> | $model | The Eloquent model FQN for the current schema. | 
| Operation | string | $operation | The current operation being performed by the schema. Usually create,edit, orview. | 
| Eloquent record | ?Illuminate\Database\Eloquent\Model | $record | The Eloquent record for the current schema. | 
| State | mixed | $state | The current value of the entry. | 
 
  By default, the content in the afterLabel() schema is aligned to the end of the container. If you wish to align it to the start of the container, you should pass a Schema::start() object containing the content:
use Filament\Infolists\Components\TextEntry;
use Filament\Schemas\Components\Icon;
use Filament\Schemas\Schema;
use Filament\Support\Icons\Heroicon;
TextEntry::make('name')
    ->afterLabel(Schema::start([
        Icon::make(Heroicon::Star),
        'This is the content after the entry\'s label'
    ]))
 除了允许静态值之外,afterLabel() 方法也可以接受函数来动态计算其值。你可以将各种 utility 作为参数注入到函数中。 
 
Learn more about utility injection.
 | Utility | Type | Parameter | Description | 
|---|---|---|---|
| Entry | Filament\Infolists\Components\Entry | $component | The current entry component instance. | 
| Get function | Filament\Schemas\Components\Utilities\Get | $get | A function for retrieving values from the current schema data. Validation is not run on form fields. | 
| Livewire | Livewire\Component | $livewire | The Livewire component instance. | 
| Eloquent model FQN | ?string<Illuminate\Database\Eloquent\Model> | $model | The Eloquent model FQN for the current schema. | 
| Operation | string | $operation | The current operation being performed by the schema. Usually create,edit, orview. | 
| Eloquent record | ?Illuminate\Database\Eloquent\Model | $record | The Eloquent record for the current schema. | 
| State | mixed | $state | The current value of the entry. | 
 
  在 Entry 标签下方添加额外内容
使用 belowLabel() 方法,你可以在 Entry 标签下方添加额外内容。你可以传入任何内容到该方法,包括文本、Schema 组件、Action 或者 Action 分组:
use Filament\Infolists\Components\TextEntry;
use Filament\Schemas\Components\Icon;
use Filament\Support\Icons\Heroicon;
TextEntry::make('name')
    ->belowLabel([
        Icon::make(Heroicon::Star),
        'This is the content below the entry\'s label'
    ])
 除了允许静态值之外,belowLabel() 方法也可以接受函数来动态计算其值。你可以将各种 utility 作为参数注入到函数中。 
 
Learn more about utility injection.
 | Utility | Type | Parameter | Description | 
|---|---|---|---|
| Entry | Filament\Infolists\Components\Entry | $component | The current entry component instance. | 
| Get function | Filament\Schemas\Components\Utilities\Get | $get | A function for retrieving values from the current schema data. Validation is not run on form fields. | 
| Livewire | Livewire\Component | $livewire | The Livewire component instance. | 
| Eloquent model FQN | ?string<Illuminate\Database\Eloquent\Model> | $model | The Eloquent model FQN for the current schema. | 
| Operation | string | $operation | The current operation being performed by the schema. Usually create,edit, orview. | 
| Eloquent record | ?Illuminate\Database\Eloquent\Model | $record | The Eloquent record for the current schema. | 
| State | mixed | $state | The current value of the entry. | 
 
  NOTE
这看起来和 aboveContent() 方法相似。但是,使用行内标签时,aboveContent() 方法会将该内容放在 Entry 上方,而不是标签下方,因为其标签显示在与 Entry 内容不同的列中。
在 Entry 内容上方添加额外内容
你可以使用 aboveContent() 方法在 Entry 内容上方插入额外内容。你可以传递任何内容 到此方法,例如文本、Schema 组件、操作或操作分组:
use Filament\Infolists\Components\TextEntry;
use Filament\Schemas\Components\Icon;
use Filament\Support\Icons\Heroicon;
TextEntry::make('name')
    ->aboveContent([
        Icon::make(Heroicon::Star),
        'This is the content above the entry\'s content'
    ])
 除了允许静态值之外,aboveContent() 方法也可以接受函数来动态计算其值。你可以将各种 utility 作为参数注入到函数中。 
 
Learn more about utility injection.
 | Utility | Type | Parameter | Description | 
|---|---|---|---|
| Entry | Filament\Infolists\Components\Entry | $component | The current entry component instance. | 
| Get function | Filament\Schemas\Components\Utilities\Get | $get | A function for retrieving values from the current schema data. Validation is not run on form fields. | 
| Livewire | Livewire\Component | $livewire | The Livewire component instance. | 
| Eloquent model FQN | ?string<Illuminate\Database\Eloquent\Model> | $model | The Eloquent model FQN for the current schema. | 
| Operation | string | $operation | The current operation being performed by the schema. Usually create,edit, orview. | 
| Eloquent record | ?Illuminate\Database\Eloquent\Model | $record | The Eloquent record for the current schema. | 
| State | mixed | $state | The current value of the entry. | 
 
  NOTE
这看起来和 belowLabel() 方法相似。不过,当使用行内标签是,belowLabel() 会将该内容放在标签下方,而不是放在 Entry 的内容上方,因为其标签显示在与 Enty 内容不同的列中。
在 Entry 内容之前添加额外内容
你可以使用 beforeContent() 方法在 Entry 内容之前插入额外内容。你可以传递任何内容 到此方法,例如文本、Schema 组件、操作或操作分组:
use Filament\Infolists\Components\TextEntry;
use Filament\Schemas\Components\Icon;
use Filament\Support\Icons\Heroicon;
TextEntry::make('name')
    ->beforeContent(Icon::make(Heroicon::Star))
 除了允许静态值之外,beforeContent() 方法也可以接受函数来动态计算其值。你可以将各种 utility 作为参数注入到函数中。 
 
Learn more about utility injection.
 | Utility | Type | Parameter | Description | 
|---|---|---|---|
| Entry | Filament\Infolists\Components\Entry | $component | The current entry component instance. | 
| Get function | Filament\Schemas\Components\Utilities\Get | $get | A function for retrieving values from the current schema data. Validation is not run on form fields. | 
| Livewire | Livewire\Component | $livewire | The Livewire component instance. | 
| Eloquent model FQN | ?string<Illuminate\Database\Eloquent\Model> | $model | The Eloquent model FQN for the current schema. | 
| Operation | string | $operation | The current operation being performed by the schema. Usually create,edit, orview. | 
| Eloquent record | ?Illuminate\Database\Eloquent\Model | $record | The Eloquent record for the current schema. | 
| State | mixed | $state | The current value of the entry. | 
 
  在条目内容之后添加额外内容
你可以使用 afterContent() 方法在条目内容后插入额外内容。你可以传递任何内容 到此方法,例如文本、Schema 组件、操作或操作组:
use Filament\Infolists\Components\TextEntry;
use Filament\Schemas\Components\Icon;
use Filament\Support\Icons\Heroicon;
TextEntry::make('name')
    ->afterContent(Icon::make(Heroicon::Star))
 除了允许静态值之外,afterContent() 方法也可以接受函数来动态计算其值。你可以将各种 utility 作为参数注入到函数中。 
 
Learn more about utility injection.
 | Utility | Type | Parameter | Description | 
|---|---|---|---|
| Entry | Filament\Infolists\Components\Entry | $component | The current entry component instance. | 
| Get function | Filament\Schemas\Components\Utilities\Get | $get | A function for retrieving values from the current schema data. Validation is not run on form fields. | 
| Livewire | Livewire\Component | $livewire | The Livewire component instance. | 
| Eloquent model FQN | ?string<Illuminate\Database\Eloquent\Model> | $model | The Eloquent model FQN for the current schema. | 
| Operation | string | $operation | The current operation being performed by the schema. Usually create,edit, orview. | 
| Eloquent record | ?Illuminate\Database\Eloquent\Model | $record | The Eloquent record for the current schema. | 
| State | mixed | $state | The current value of the entry. | 
 
  向条目添加额外的 HTML 属性
你可以通过 extraAttributes() 方法向条目传递额外的 HTML 属性,这些属性将被合并到其外层 HTML 元素中。这些属性应以数组的形式表示,其中键为属性名称,值是属性值:
use Filament\Infolists\Components\TextEntry;
TextEntry::make('slug')
    ->extraAttributes(['class' => 'bg-gray-200'])
 除了允许静态值之外,extraAttributes() 方法也可以接受函数来动态计算其值。你可以将各种 utility 作为参数注入到函数中。 
 
Learn more about utility injection.
 | Utility | Type | Parameter | Description | 
|---|---|---|---|
| Entry | Filament\Infolists\Components\Entry | $component | The current entry component instance. | 
| Get function | Filament\Schemas\Components\Utilities\Get | $get | A function for retrieving values from the current schema data. Validation is not run on form fields. | 
| Livewire | Livewire\Component | $livewire | The Livewire component instance. | 
| Eloquent model FQN | ?string<Illuminate\Database\Eloquent\Model> | $model | The Eloquent model FQN for the current schema. | 
| Operation | string | $operation | The current operation being performed by the schema. Usually create,edit, orview. | 
| Eloquent record | ?Illuminate\Database\Eloquent\Model | $record | The Eloquent record for the current schema. | 
| State | mixed | $state | The current value of the entry. | 
默认情况下,多次调用 extraAttributes() 将会覆盖之前的属性。如果你想要合并这些属性,可以传递 merge: true 到该方法中。
向条目包装器添加额外的 HTML 属性
你还可以将额外的 HTML 属性传递给“条目包装器”的最外层元素,该元素包围着条目的标签和内容。如果你想通过 CSS 设置条目的标签或间距样式,这将非常有用,因为你可以将元素定位为包装器的子元素:
use Filament\Infolists\Components\TextEntry;
TextEntry::make('slug')
    ->extraEntryWrapperAttributes(['class' => 'components-locked'])
 除了允许静态值之外,extraEntryWrapperAttributes() 方法也可以接受函数来动态计算其值。你可以将各种 utility 作为参数注入到函数中。 
 
Learn more about utility injection.
 | Utility | Type | Parameter | Description | 
|---|---|---|---|
| Entry | Filament\Infolists\Components\Entry | $component | The current entry component instance. | 
| Get function | Filament\Schemas\Components\Utilities\Get | $get | A function for retrieving values from the current schema data. Validation is not run on form fields. | 
| Livewire | Livewire\Component | $livewire | The Livewire component instance. | 
| Eloquent model FQN | ?string<Illuminate\Database\Eloquent\Model> | $model | The Eloquent model FQN for the current schema. | 
| Operation | string | $operation | The current operation being performed by the schema. Usually create,edit, orview. | 
| Eloquent record | ?Illuminate\Database\Eloquent\Model | $record | The Eloquent record for the current schema. | 
| State | mixed | $state | The current value of the entry. | 
默认情况下,多次调用 extraEntryWrapperAttributes() 将会覆盖之前的属性。如果你想要合并这些属性,可以传递 merge: true 到该方法中。
Entry utility 注入
用于配置 Entry 的绝大多数方法都接受函数作为参数,而不是硬编码值:
use App\Models\User;
use Filament\Infolists\Components\TextEntry;
TextEntry::make('name')
    ->label(fn (string $state): string => str_contains($state, ' ') ? 'Full name' : 'Name')
TextEntry::make('currentUserEmail')
    ->state(fn (): string => auth()->user()->email)
TextEntry::make('role')
    ->hidden(fn (User $record): bool => $record->role === 'admin')
仅凭这一点就解锁了许多自定义可能性。
该包还可以注入许多 utility,以作为这些函数内部的参数使用。所有接受函数作为参数的自定义方法都可以注入 utility。
这些注入的 utility 需要使用特定的参数名称。否则,Filament 将无从知晓要注入的是什么。
注入条目的当前状态
如果你想访问字段的当前值(状态),请定义 $state 参数:
function ($state) {
    // ...
}
注入其他条目或表单字段的状态
使用 $get 参数,你可以在回调函数中检索其他条目或表单字段的状态(值):
use Filament\Schemas\Components\Utilities\Get;
function (Get $get) {
    $email = $get('email'); // Store the value of the `email` entry in the `$email` variable.
    //...
}
TIP
除非表单字段是响应式的,当字段值改变时,Schema 不会刷新,而只会在用户下一次交互发起请求到服务器时才会刷新。如果你需要字段值响应式变更,请使用 live()。
注入当前 Eloquent 记录
你可以使用 $record 参数,检索当前 Schma 的 Eloquent 记录:
use Illuminate\Database\Eloquent\Model;
function (?Model $record) {
    // ...
}
注入当前操作
如果你正在为面板资源或关联管理器编写 Schema ,并且希望检查 Schema 是否为 create、edit 或者 view,请使用 $operation 参数:
function (string $operation) {
    // ...
}
NOTE
使用 $schema->operation() 方法,手动设置 Schema 的操作。
注入当前 Livewire 组件实例
如果你想访问当前 Livewire 组件实例,请定义 $livewire 参数:
use Livewire\Component;
function (Component $livewire) {
    // ...
}
注入当前条目实例
如果你想访问当前组件实例,请定义 $component 参数:
use Filament\Infolists\Components\Entry;
function (Entry $component) {
    // ...
}
注入各种 utility
参数使用反射动态注入,因此你可以以任何顺序联合使用多个参数:
use App\Models\User;
use Filament\Schemas\Components\Utilities\Get;
use Livewire\Component as Livewire;
function (Livewire $livewire, Get $get, User $record) {
    // ...
}
注入来自 Laravel 容器的依赖
你可以像平常一样注入来自 Laravel 的容器的任何东西,以及 utility:
use App\Models\User;
use Illuminate\Http\Request;
function (Request $request, User $record) {
    // ...
}
全局设置
如果你希望全局修改所有 Entry 的默认行为,那么你可以在服务提供者的 boot() 方法或者中间件中调用静态的 configureUsing() 方法。传入一个可以修改 Entry 的闭包。比如你想让所有的 TextEntry 组件都设为 words[10],你可以这样设置:
use Filament\Infolists\Components\TextEntry;
TextEntry::configureUsing(function (TextEntry $entry): void {
    $entry->words(10);
});
当然,你仍然可以单独在每个 Entry 中重写该方法:
use Filament\Infolists\Components\TextEntry;
TextEntry::make('name')
    ->words(null)
Still need help? Join our Discord community or open a GitHub discussion
 
  
  
  
  
  
  
  
  
  
  
  
  
  
  
  
  
  
  
 