CreateEsg.php 1.2KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344
  1. <?php
  2. namespace App\Filament\Resources\EsgResource\Pages;
  3. use App\Filament\Resources\EsgResource;
  4. use App\Traits\HandlesEsgParagraphs;
  5. use Filament\Actions;
  6. use Filament\Resources\Pages\CreateRecord;
  7. use Illuminate\Database\Eloquent\Model;
  8. class CreateEsg extends CreateRecord
  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. protected function handleRecordCreation(array $data): Model
  17. {
  18. \Log::info("=== 開始建立流程 ===");
  19. // ✅ 使用 Trait 方法處理表單中的 paragraphs
  20. $processedParagraphs = $this->processFormParagraphs();
  21. if ($processedParagraphs) {
  22. \Log::info("預處理後的 paragraphs:", $processedParagraphs);
  23. }
  24. // 建立主記錄
  25. $record = static::getModel()::create($data);
  26. \Log::info("主記錄已建立:", ['id' => $record->id]);
  27. // ✅ 使用 Trait 方法建立 paragraphs 關聯
  28. if ($processedParagraphs) {
  29. $this->createParagraphs($record, $processedParagraphs);
  30. }
  31. return $record->refresh();
  32. }
  33. }