EditEsg.php 1.3KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849
  1. <?php
  2. namespace App\Filament\Resources\EsgResource\Pages;
  3. use App\Filament\Resources\EsgResource;
  4. use Filament\Actions;
  5. use Filament\Resources\Pages\EditRecord;
  6. use Illuminate\Database\Eloquent\Model;
  7. use App\Traits\HandlesEsgParagraphs;
  8. class EditEsg extends EditRecord
  9. {
  10. use HandlesEsgParagraphs;
  11. protected static string $resource = EsgResource::class;
  12. protected function getRedirectUrl(): string
  13. {
  14. return $this->getResource()::getUrl('index');
  15. }
  16. /**
  17. * 在填充表單前處理資料 - 手動載入關聯資料
  18. */
  19. protected function mutateFormDataBeforeFill(array $data): array
  20. {
  21. // ✅ 使用 Trait 方法載入並格式化 paragraphs
  22. $data['paragraphs'] = $this->loadAndFormatParagraphs($this->record);
  23. return $data;
  24. }
  25. /**
  26. * 儲存時的處理
  27. */
  28. protected function handleRecordUpdate(Model $record, array $data): Model
  29. {
  30. // ✅ 使用 Trait 方法處理表單中的 paragraphs
  31. $processedParagraphs = $this->processFormParagraphs();
  32. // 更新主記錄
  33. $record->update($data);
  34. // ✅ 使用 Trait 方法處理 paragraphs 關聯
  35. if ($processedParagraphs) {
  36. $this->updateParagraphs($record, $processedParagraphs);
  37. }
  38. return $record->refresh();
  39. }
  40. }