'array', // JSON 轉陣列 ]; public function news(){ return $this->belongsTo(News::class); } public function contentType() { switch ($this->paragraph_type){ case 1: return "image"; case 2: return "text"; case 3: return "video"; default: return ""; } } public function photos(){ return $this->hasMany(NewsPhoto::class)->orderBy("order"); } protected function paragraphVideoType(): Attribute { return Attribute::make( get: fn ($value) => ($this->paragraph_type == 3 && !is_null($this->video_type)) ? ($this->video_type == 1 ? "url" : "upload") : null, ); } protected function paragraphVideoImg(): Attribute { return Attribute::make( get: fn ($value) => is_null($this->video_img) ? null :Storage::disk('public')->url($this->video_img), ); } protected function paragraphVideoUrl(): Attribute { return Attribute::make( get: fn ($value) => ($this->video_type == 2) ? Storage::disk('public')->url($this->video_url) : $this->link, ); } /** * 取得特定語言的文字內容 */ public function getTextContent($locale = 'zh_TW'): string { if ($this->paragraph_type != 2) { return ''; } $content = $this->text_content; if (is_array($content)) { return $content[$locale] ?? ''; } if (is_string($content)) { $decoded = json_decode($content, true); return $decoded[$locale] ?? ''; } return ''; } }