recordId = (int) $request->route('record'); if (!$this->recordId) { abort(404); } // 从API获取教材数据 $apiService = app(TextbookApiService::class); $textbookData = $apiService->getTextbook($this->recordId); if (!$textbookData) { abort(404); } // 初始化表单数据 $this->data = $textbookData; } public function save(): void { // 验证数据 $this->validate([ 'data.series_id' => 'required|integer', 'data.stage' => 'required|string', 'data.grade' => 'nullable|integer', 'data.semester' => 'nullable|integer', 'data.official_title' => 'required|string|max:255', 'data.isbn' => 'nullable|string|max:255', 'data.status' => 'required|string|in:draft,published,archived', ]); // 只传递标量字段到API,跳过关系字段和嵌套对象 $allowedFields = [ 'series_id', 'stage', 'grade', 'semester', 'naming_scheme', 'track', 'module_type', 'volume_no', 'legacy_code', 'curriculum_standard_year', 'curriculum_revision_year', 'approval_authority', 'approval_year', 'edition_label', 'official_title', 'aliases', 'isbn', 'cover_path', 'status', 'sort_order', 'meta' ]; $updateData = []; foreach ($allowedFields as $field) { if (isset($this->data[$field]) && !is_array($this->data[$field]) && !is_object($this->data[$field])) { $updateData[$field] = $this->data[$field]; } } // 调用API更新 $apiService = app(TextbookApiService::class); $updatedData = $apiService->updateTextbook($this->recordId, $updateData); // 更新本地数据 $this->data = array_merge($this->data, $updatedData); // 显示成功消息 \Filament\Notifications\Notification::make() ->title('教材更新成功') ->success() ->send(); } protected function getHeaderActions(): array { return [ Actions\Action::make('back') ->label('返回列表') ->url(static::$resource::getUrl('index')) ->color('gray'), ]; } public function getTitle(): string { return '编辑教材'; } public function getView(): string { return 'filament.resources.textbook-resource.edit'; } public function form(\Filament\Forms\Form $form): \Filament\Forms\Form { return $form ->schema([ \Filament\Forms\Components\Section::make('基本信息') ->schema([ \Filament\Forms\Components\Select::make('data.series_id') ->label('教材系列') ->options(function () { $apiService = app(TextbookApiService::class); $series = $apiService->getTextbookSeries(); $options = []; foreach ($series['data'] as $s) { $options[$s['id']] = $s['name']; } return $options; }) ->required() ->searchable(), \Filament\Forms\Components\Select::make('data.stage') ->label('学段') ->options([ 'primary' => '小学', 'junior' => '初中', 'senior' => '高中', ]) ->required() ->reactive(), \Filament\Forms\Components\TextInput::make('data.grade') ->label('年级') ->numeric() ->helperText('例如:7表示七年级'), \Filament\Forms\Components\Select::make('data.semester') ->label('学期') ->options([ 1 => '上学期', 2 => '下学期', ]) ->helperText('选择学期'), \Filament\Forms\Components\TextInput::make('data.official_title') ->label('教材名称') ->required() ->maxLength(255), \Filament\Forms\Components\TextInput::make('data.isbn') ->label('ISBN') ->maxLength(255), \Filament\Forms\Components\Select::make('data.status') ->label('状态') ->options([ 'draft' => '草稿', 'published' => '已发布', 'archived' => '已归档', ]) ->required(), ]) ->columns(2), ]) ->statePath('data'); } /** * 获取表单实例供视图使用 */ public function getFormProperty(): string { // 直接渲染表单HTML ob_start(); try { $form = $this->form(); $form->render(); } catch (\Exception $e) { // 忽略表单渲染错误 } return ob_get_clean(); } }