Filament:在一个不同的模型上展示一个viewAction

后端开发 2026-07-11

在Laravel 12中使用Filament 5。我有一辆车可能会有若干损坏。在viewVehicle页面,我做了一个自定义视图,用来在地图上显示所有损坏。

Section::make(__('Damage map'))
                    ->schema([
                        ViewEntry::make('damage_map')
                            ->hiddenLabel()
                            ->view('filament.components.vehicle-damage-map')
                            ->viewData(fn (Vehicle $record): array => [
                                'imageUrl' => VehicleDamageMap::getVehicleTypeImageUrl($record),
                                'points' => VehicleDamageMap::getDamagePoints($record),
                            ])
                            ->columnSpanFull(),
                    ])
                    ->columnSpanFull()
                    ->columns(12),

Blade只是遍历这些点,把它们显示为一个点,我把它实现成一个动作

@foreach ($points as $point)
            {{ ($this->viewDamageAction)(['record' => $point]) }}
        @endforeach

现在问题来了,我尝试了好几种方法,但view action,因其在VehicleView内部完成,总是取用车辆的数据来填充表单,忽略了fillForm方法

return ViewAction::make('viewDamage')
->fillForm(function ($arguments, $form): array {
                $record = VehicleDamage::find($arguments['record']['id']);
                return $record->toArray();
            })
->schema(function ($schema, $arguments) {
                return VehicleDamageInfolist::configure($schema);
            });

因此我得到的是来自损坏的结构,包含损坏信息列表的所有正确字段,但显示的数据却是车辆的数据。

我到底哪里做错了?

解决方案

Solved! It's not fillForm but record in this case.

->record(fn (array $arguments) => VehicleDamage::find($arguments['record']['id']))
站内所有文章版权归属LeftHeroAI导航站,无授权禁止任何主体转载、抄袭、复制内容,亦不得私自架设镜像站点。一经侵权,本站将通过法律途径追责。

相关文章