'integer', 'image_path' => 'json', // Laravel auto-decodes JSON → array ]; // === MUTATOR: Always return array (even if null) === public function getImagePathAttribute($value): array { if (is_null($value) || $value === '' || $value === 'null') { return []; } // If already array (from cast 'json') if (is_array($value)) { return $value; } // Fallback: decode string (old data) $decoded = json_decode($value, true); return is_array($decoded) ? $decoded : [$value]; } // === SETTER: Always save as JSON === public function setImagePathAttribute($value): void { if (is_array($value)) { $filtered = array_filter($value); // remove null/empty $this->attributes['image_path'] = !empty($filtered) ? json_encode($filtered) : null; } else { $this->attributes['image_path'] = null; } } // === THUMBNAIL: First image as string (for homepage) === public function getThumbnailAttribute(): ?string { return $this->image_path[0] ?? null; } // === IMAGE COUNT === public function getImageCountAttribute(): int { return count($this->image_path); } // === RELATIONSHIP === public function categories(): BelongsToMany { return $this->belongsToMany(Category::class, 'project_category'); } // === BILINGUAL ACCESSORS === public function getTitleAttribute(): string { return $this->{'title_' . App::getLocale()} ?? $this->title_en; } public function getShortDescriptionAttribute(): ?string { return $this->{'short_description_' . App::getLocale()} ?? $this->short_description_en; } public function getDescriptionAttribute(): ?string { return $this->{'description_' . App::getLocale()} ?? $this->description_en; } public function getImageAltAttribute(): ?string { return $this->{'image_alt_' . App::getLocale()} ?? $this->image_alt_en; } public function getLocationAttribute(): ?string { return $this->{'location_' . App::getLocale()} ?? $this->location_en; } }