hasMany(TestimonialTranslation::class, 'testimonial_id'); } /** * Present testimonials in the same shape as Service and Skill: * * ['client_name' => ['fa' => '...', 'en' => '...']] * * Testimonials store translations in a child table rather than a JSON * column, so without this the admin panel — and the generic resource form, * which reads `translations[key][locale]` — would receive a flat list of * rows instead and silently render every field blank. * * The relation has to be read through getRelation(): a plain * `$this->translations` would re-enter this very accessor. * * @return array> */ public function getTranslationsAttribute(): array { $rows = $this->relationLoaded('translations') ? $this->getRelation('translations') : $this->translations()->get(); $columns = ['client_name', 'client_position', 'feedback']; $shaped = []; foreach ($columns as $column) { foreach (['fa', 'en'] as $locale) { $shaped[$column][$locale] = $rows->firstWhere('locale', $locale)?->{$column}; } } return $shaped; } /** * toArray() would otherwise serialise the relation as a list of rows and * bypass the accessor above, so the read API and the write responses would * disagree with each other and with the other translatable models. */ public function toArray(): array { return array_merge(parent::toArray(), ['translations' => $this->translations]); } }