要使用视图页面创建新资源,可以使用 --view 标志:
php artisan make:filament-resource User --view默认情况下,视图页面将显示一个包含记录数据的禁用表单。如果您更倾向于在一个“信息列表”中显示记录的数据,您可以在资源类上定义一个 infolist() 方法:
use Filament\Infolists;
use Filament\Schemas\Schema;
public static function infolist(Schema $schema): Schema
{
return $schema
->components([
Infolists\Components\TextEntry::make('name'),
Infolists\Components\TextEntry::make('email'),
Infolists\Components\TextEntry::make('notes')
->columnSpanFull(),
]);
}components() 方法用于定义信息列表的结构。它是一个包含 条目 和 布局组件 的数组,按照它们在信息列表中应该出现的顺序排列。
查看 Infolists 文档,以获取一份 指南 关于如何使用 Filament 构建信息列表。
如果你想为一个现有资源添加一个视图页面,在你的资源的 Pages 目录中创建一个新页面:
php artisan make:filament-page ViewUser --resource=UserResource --type=ViewRecord您必须在您的资源的getPages()方法中注册这个新页面:
public static function getPages(): array
{
return [
'index' => Pages\ListUsers::route('/'),
'create' => Pages\CreateUser::route('/create'),
'view' => Pages\ViewUser::route('/{record}'),
'edit' => Pages\EditUser::route('/{record}/edit'),
];
}如果您的资源很简单,您可能希望在模态框中查看记录,而不是在查看页面上。如果是这样,您只需删除查看页面即可。
如果你的资源不包含一个ViewAction, 你可以将它添加到$table->recordActions() array:
use Filament\Actions\ViewAction;
use Filament\Tables\Table;
public static function table(Table $table): Table
{
return $table
->columns([
// ...
])
->recordActions([
ViewAction::make(),
// ...
]);
}你可能希望在数据填充到表单之前,修改来自记录的数据。为此,你可以在 View 页面类上定义一个 mutateFormDataBeforeFill() 方法来修改 $data 数组,并在数据填充到表单之前返回修改后的版本:
protected function mutateFormDataBeforeFill(array $data): array
{
$data['user_id'] = auth()->id();
return $data;
}或者,如果您正在模态操作中查看记录,请查看操作文档。
钩子可用于在页面生命周期的不同阶段执行代码,例如在表单填写之前。要设置钩子,在 View 页面类上创建一个受保护方法,以钩子名称命名:
use Filament\Resources\Pages\ViewRecord;
class ViewUser extends ViewRecord
{
// ...
protected function beforeFill(): void
{
// Runs before the disabled form fields are populated from the database. Not run on pages using an infolist.
}
protected function afterFill(): void
{
// Runs after the disabled form fields are populated from the database. Not run on pages using an infolist.
}
}为了授权,Filament 将会遵循任何在您的应用中注册的模型策略。
用户可以访问 View 页面 如果 view() 模型策略的 方法 返回 true 。
一个 View 页面可能不足以让用户浏览大量信息。你可以为一个资源创建任意数量的 View 页面。如果你正在使用 资源子导航,这会特别有用,因为你随后可以轻松地在不同的 View 页面之间切换。
要创建一个“查看”页面,您应该使用 make:filament-page 命令:
php artisan make:filament-page ViewCustomerContact --resource=CustomerResource --type=ViewRecord你必须在资源的getPages()方法中注册这个新页面:
public static function getPages(): array
{
return [
'index' => Pages\ListCustomers::route('/'),
'create' => Pages\CreateCustomer::route('/create'),
'view' => Pages\ViewCustomer::route('/{record}'),
'view-contact' => Pages\ViewCustomerContact::route('/{record}/contact'),
'edit' => Pages\EditCustomer::route('/{record}/edit'),
];
}现在,你可以定义此页面的 infolist() 或 form(),其中可以包含主查看页面上不存在的其他组件:
use Filament\Schemas\Schema;
public function infolist(Schema $schema): Schema
{
return $schema
->components([
// ...
]);
}您可以通过定义一个 getAllRelationManagers() 方法来指定在视图页面上应显示哪些关系管理器:
protected function getAllRelationManagers(): array
{
return [
CustomerAddressesRelationManager::class,
CustomerContactsRelationManager::class,
];
}这在您有 多个视图页面 并且需要不同的关系管理器在
每个页面上时非常有用:
// ViewCustomer.php
protected function getAllRelationManagers(): array
{
return [
RelationManagers\OrdersRelationManager::class,
RelationManagers\SubscriptionsRelationManager::class,
];
}
// ViewCustomerContact.php
protected function getAllRelationManagers(): array
{
return [
RelationManagers\ContactsRelationManager::class,
RelationManagers\AddressesRelationManager::class,
];
}如果 getAllRelationManagers() 未定义,将使用资源中定义的任何关系管理器。
如果你正在使用资源子导航,你可以像往常一样在资源的getRecordSubNavigation()中注册此页面:
use App\Filament\Resources\Customers\Pages;
use Filament\Resources\Pages\Page;
public static function getRecordSubNavigation(Page $page): array
{
return $page->generateNavigationItems([
// ...
Pages\ViewCustomerContact::class,
]);
}Filament 中的每个页面都有自己的 模式,它定义了整体结构和内容。你可以通过为页面定义 content() 方法来覆盖其模式。View 页面的 content() 方法默认包含以下组件:
use Filament\Schemas\Schema;
public function content(Schema $schema): Schema
{
return $schema
->components([
$this->hasInfolist() // This method returns `true` if the page has an infolist defined
? $this->getInfolistContentComponent() // This method returns a component to display the infolist that is defined in this resource
: $this->getFormContentComponent(), // This method returns a component to display the form that is defined in this resource
$this->getRelationManagersContentComponent(), // This method returns a component to display the relation managers that are defined in this resource
]);
}在components()数组中,您可以插入任何schema 组件. 您可以通过改变数组的顺序来重新排序组件,或者移除任何不需要的组件
为了进一步自定义,你可以覆盖页面类上的静态 $view 属性为一个在你应用中的自定义视图:
protected string $view = 'filament.resources.users.pages.view-user';这假设您已经在 resources/views/filament/resources/users/pages/view-user.blade.php 处创建了一个视图:
<x-filament-panels::page>
{{-- `$this->getRecord()` will return the current Eloquent record for this page --}}
{{ $this->content }} {{-- This will render the content of the page defined in the `content()` method, which can be removed if you want to start from scratch --}}
</x-filament-panels::page>