1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253 |
- <?php
-
- namespace App\Models;
-
- use Illuminate\Database\Eloquent\Casts\Attribute;
- use Illuminate\Database\Eloquent\Factories\HasFactory;
- use Illuminate\Database\Eloquent\Model;
- use Illuminate\Database\Eloquent\Relations\BelongsTo;
- use Illuminate\Database\Eloquent\SoftDeletes;
- use Illuminate\Support\Facades\Storage;
- use Spatie\Translatable\HasTranslations;
-
- class EsgParagraph extends Model
- {
- use HasFactory, HasTranslations;
- //
- protected $fillable = [
- 'esg_id',
- 'type',
- 'order',
- 'content',
- ];
-
- protected $casts = [
- 'content' => 'array', // 自動轉換 JSON
- 'type' => 'integer',
- 'order' => 'integer'
- ];
-
- public function esg(): BelongsTo
- {
- return $this->belongsTo(Esg::class);
- }
-
- // ✅ 取得段落類型名稱
- public function getTypeNameAttribute(): string
- {
- return match($this->type) {
- 1 => '純文字',
- 2 => '區塊文字',
- 3 => '表格',
- 4 => '圖片',
- default => '未知',
- };
- }
-
- // 如果你想要更細緻的控制,可以使用 accessor 和 mutator
- public function getContentAttribute($value)
- {
- return $value ? json_decode($value, true) : [];
- }
- }
|