These three had read endpoints or nothing at all, so their content could only be changed by hand in the database. All writes are admin-only and rate limited like the existing admin routes. Testimonials are the awkward one: the migration that collapsed the legacy per-locale tables into JSON columns deliberately skipped them, so they still live in a child table. Testimonial therefore exposes a translations accessor to present the same translations[key][locale] shape as Service and Skill, which is what the generic resource form binds to. Reading the child relation has to go through getRelation(), since a plain $this->translations inside an accessor of that name re-enters the accessor and silently yields nulls. The store requests now mark the NOT NULL columns as required. Leaving them nullable let a bad payload reach the database and come back as a 500 instead of a 422, and about_me_id is additionally a cascading foreign key. Also registers the three resources with the admin read endpoint so the panel can list them, eager loading the relations their forms need.
69 lines
2.0 KiB
PHP
69 lines
2.0 KiB
PHP
<?php
|
|
|
|
namespace App\Models\Website;
|
|
|
|
use Illuminate\Database\Eloquent\Factories\HasFactory;
|
|
use Illuminate\Database\Eloquent\Model;
|
|
use Illuminate\Database\Eloquent\Relations\HasMany;
|
|
|
|
class Testimonial extends Model
|
|
{
|
|
use HasFactory;
|
|
|
|
protected $table = 'website_testimonials';
|
|
|
|
protected $fillable = [
|
|
'image',
|
|
'client_link',
|
|
];
|
|
|
|
public function translations(): HasMany
|
|
{
|
|
return $this->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<string, array<string, string|null>>
|
|
*/
|
|
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]);
|
|
}
|
|
}
|