74 lines
1.6 KiB
PHP
74 lines
1.6 KiB
PHP
<?php
|
|
|
|
namespace App\Models;
|
|
|
|
use Illuminate\Database\Eloquent\Builder;
|
|
use Illuminate\Database\Eloquent\Factories\HasFactory;
|
|
use Illuminate\Database\Eloquent\Model;
|
|
use Spatie\Translatable\HasTranslations;
|
|
|
|
class CmsArticle extends Model
|
|
{
|
|
use HasFactory, HasTranslations;
|
|
|
|
protected $table = 'cms_articles';
|
|
|
|
protected $fillable = [
|
|
'slug',
|
|
'title',
|
|
'subtitle',
|
|
'image',
|
|
'category',
|
|
'date_label',
|
|
'read_time',
|
|
'author',
|
|
'content',
|
|
'is_published',
|
|
'order',
|
|
];
|
|
|
|
/** @var array<string> */
|
|
public array $translatable = [
|
|
'title',
|
|
'subtitle',
|
|
'content',
|
|
];
|
|
|
|
protected function casts(): array
|
|
{
|
|
return [
|
|
'author' => 'array',
|
|
'is_published' => 'boolean',
|
|
'order' => 'integer',
|
|
];
|
|
}
|
|
|
|
public function scopePublished(Builder $query): Builder
|
|
{
|
|
return $query->where('is_published', true);
|
|
}
|
|
|
|
public function scopeOrdered(Builder $query): Builder
|
|
{
|
|
return $query->orderBy('order')->orderByDesc('created_at');
|
|
}
|
|
|
|
/**
|
|
* @return array<string, mixed>
|
|
*/
|
|
public function toFrontendArray(): array
|
|
{
|
|
return [
|
|
'id' => $this->id,
|
|
'slug' => $this->slug,
|
|
'title' => $this->title,
|
|
'subtitle' => $this->subtitle,
|
|
'image' => $this->image,
|
|
'category' => $this->category,
|
|
'date' => $this->date_label,
|
|
'readTime' => $this->read_time,
|
|
'author' => $this->author ?? [],
|
|
'content' => $this->content ?? [],
|
|
];
|
|
}
|
|
}
|