Files
2026-08-08 18:03:29 +03:00

68 lines
1.6 KiB
PHP

<?php
namespace App\Models;
use Database\Factories\PageFactory;
use Illuminate\Database\Eloquent\Attributes\Fillable;
use Illuminate\Database\Eloquent\Attributes\RouteKey;
use Illuminate\Database\Eloquent\Attributes\Scope;
use Illuminate\Database\Eloquent\Builder;
use Illuminate\Database\Eloquent\Casts\Attribute;
use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Support\Str;
#[Fillable([
'title', 'slug', 'body', 'meta_title', 'meta_description',
'published', 'show_in_footer', 'order',
])]
#[RouteKey('slug')]
class Page extends Model
{
/** @use HasFactory<PageFactory> */
use HasFactory;
protected function casts(): array
{
return [
'published' => 'boolean',
'show_in_footer' => 'boolean',
];
}
protected static function booted(): void
{
static::creating(function (Page $page) {
if (empty($page->slug)) {
$page->slug = Str::slug($page->title);
}
});
}
/**
* @param Builder<Page> $query
*/
#[Scope]
protected function published(Builder $query): void
{
$query->where('published', true);
}
/**
* @param Builder<Page> $query
*/
#[Scope]
protected function inFooter(Builder $query): void
{
$query->where('show_in_footer', true)->orderBy('order')->orderBy('title');
}
protected function bodyHtml(): Attribute
{
return Attribute::get(fn () => Str::markdown($this->body, [
'html_input' => 'strip',
'allow_unsafe_links' => false,
]));
}
}