@@ -0,0 +1,63 @@
|
||||
<?php
|
||||
|
||||
namespace App\Models;
|
||||
|
||||
use Illuminate\Database\Eloquent\Attributes\Fillable;
|
||||
use Illuminate\Database\Eloquent\Builder;
|
||||
use Illuminate\Database\Eloquent\Model;
|
||||
|
||||
/**
|
||||
* A client testimonial, written in both site languages.
|
||||
*
|
||||
* @property int $id
|
||||
* @property string $quote_id
|
||||
* @property string $quote_en
|
||||
* @property string $role_id
|
||||
* @property string $role_en
|
||||
* @property string $organization_id
|
||||
* @property string $organization_en
|
||||
* @property bool $is_published
|
||||
* @property int $sort_order
|
||||
*/
|
||||
#[Fillable([
|
||||
'quote_id', 'quote_en', 'role_id', 'role_en',
|
||||
'organization_id', 'organization_en', 'is_published', 'sort_order',
|
||||
])]
|
||||
class Testimonial extends Model
|
||||
{
|
||||
/**
|
||||
* @return array<string, string>
|
||||
*/
|
||||
protected function casts(): array
|
||||
{
|
||||
return [
|
||||
'is_published' => 'boolean',
|
||||
'sort_order' => 'integer',
|
||||
];
|
||||
}
|
||||
|
||||
/**
|
||||
* @param Builder<Testimonial> $query
|
||||
*/
|
||||
public function scopePublished(Builder $query): void
|
||||
{
|
||||
$query->where('is_published', true)->orderBy('sort_order')->orderBy('id');
|
||||
}
|
||||
|
||||
/**
|
||||
* The testimonial in the visitor's language.
|
||||
*
|
||||
* @return array{id: int, quote: string, role: string, organization: string}
|
||||
*/
|
||||
public function localized(string $locale): array
|
||||
{
|
||||
$suffix = $locale === 'en' ? 'en' : 'id';
|
||||
|
||||
return [
|
||||
'id' => $this->id,
|
||||
'quote' => $this->{"quote_{$suffix}"},
|
||||
'role' => $this->{"role_{$suffix}"},
|
||||
'organization' => $this->{"organization_{$suffix}"},
|
||||
];
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user