EsgParagraph.php 1.2KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253
  1. <?php
  2. namespace App\Models;
  3. use Illuminate\Database\Eloquent\Casts\Attribute;
  4. use Illuminate\Database\Eloquent\Factories\HasFactory;
  5. use Illuminate\Database\Eloquent\Model;
  6. use Illuminate\Database\Eloquent\Relations\BelongsTo;
  7. use Illuminate\Database\Eloquent\SoftDeletes;
  8. use Illuminate\Support\Facades\Storage;
  9. use Spatie\Translatable\HasTranslations;
  10. class EsgParagraph extends Model
  11. {
  12. use HasFactory, HasTranslations;
  13. //
  14. protected $fillable = [
  15. 'esg_id',
  16. 'type',
  17. 'order',
  18. 'content',
  19. ];
  20. protected $casts = [
  21. 'content' => 'array', // 自動轉換 JSON
  22. 'type' => 'integer',
  23. 'order' => 'integer'
  24. ];
  25. public function esg(): BelongsTo
  26. {
  27. return $this->belongsTo(Esg::class);
  28. }
  29. // ✅ 取得段落類型名稱
  30. public function getTypeNameAttribute(): string
  31. {
  32. return match($this->type) {
  33. 1 => '純文字',
  34. 2 => '區塊文字',
  35. 3 => '表格',
  36. 4 => '圖片',
  37. default => '未知',
  38. };
  39. }
  40. // 如果你想要更細緻的控制,可以使用 accessor 和 mutator
  41. public function getContentAttribute($value)
  42. {
  43. return $value ? json_decode($value, true) : [];
  44. }
  45. }