公告: 旧版文档请移步 -> http://v2.laravel-filament.cn/docs

Languages

Version

Theme

面板构建器 - 资源

查看记录

创建带有查看页的资源

要想创建一个带有查看页面的新资源,你可以使用 --view 标志:

php artisan make:filament-resource User --view

使用信息列表而非禁用的表单

默认情况下,查看页会使用禁用的表单展示记录数据。如果你倾向于在 "infolist" 展示数据,你可以在资源类中定义 infolist() 方法:

use Filament\Infolists;
use Filament\Infolists\Infolist;
 
public static function infolist(Infolist $infolist): Infolist
{
return $infolist
->schema([
Infolists\Components\TextEntry::make('name'),
Infolists\Components\TextEntry::make('email'),
Infolists\Components\TextEntry::make('notes')
->columnSpanFull(),
]);
}

schema() 方法用于定义消息列表的结构。它是一个 entries布局组件的数组,按照显示的顺序排序。

请查阅消息列表文档,查看如何使用 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->actions() 数组中:

use Filament\Tables;
use Filament\Tables\Table;
 
public static function table(Table $table): Table
{
return $table
->columns([
// ...
])
->actions([
Tables\Actions\ViewAction::make(),
// ...
]);
}

填充表单前自定义数据

你可能想要在记录填充到表单前修改数据。你可以通过定义查看页类的 mutateFormDataBeforeFill() 方法去修改 $data 数组,并在填充到表单前返回修改后的版本:

protected function mutateFormDataBeforeFill(array $data): array
{
$data['user_id'] = auth()->id();
 
return $data;
}

此外,如果你想在模态框 Action 中查看记录,请查阅 Action 文档

授权

关于授权,Filament 会监听所有应用中注册的模型策略

如果模型策略的 view() 方法中返回的是 true(),用户可以访问查看页。

创建另一个查看页

一个编辑页可能空间不过,无法让用户浏览所有信息。你为资源创建任意数量的查看页。如果你正在使用资源子导航,这一点尤其有用,因为你可以轻松地在不同的查看页之间切换。

要创建查看页,你可以使用 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\Infolists\Infolist;
 
public function infolist(Infolist $infolist): Infolist
{
return $infolist
->schema([
// ...
]);
}

将查看页添加到资源子导航

如果你使用资源子导航,你可以在该资源的 getRecordSubNavigation() 中注册该页面:

use App\Filament\Resources\CustomerResource\Pages;
use Filament\Resources\Pages\Page;
 
public static function getRecordSubNavigation(Page $page): array
{
return $page->generateNavigationItems([
// ...
Pages\ViewCustomerContact::class,
]);
}

自定义视图

要进一步自定义,你可以重写页面类的静态 $view 属性:

protected static string $view = 'filament.resources.users.pages.view-user';

这假定了你已经创建了一个视图文件 resources/views/filament/resources/users/pages/list-users.blade.php

下例是视图可能包含内容的简单示例:

<x-filament::page>
@if ($this->hasInfolist())
{{ $this->infolist }}
@else
{{ $this->form }}
@endif
 
@if (count($relationManagers = $this->getRelationManagers()))
<x-filament::resources.relation-managers
:active-manager="$this->activeRelationManager"
:managers="$relationManagers"
:owner-record="$record"
:page-class="static::class"
/>
@endif
</x-filament::page>

要查看默认视图中包含的内容,你可以查看项目的 vendor/filament/filament/resources/views/resources/pages/view-records.blade.php 文件。

Edit on GitHub

Still need help? Join our Discord community or open a GitHub discussion