first commit
This commit is contained in:
@@ -0,0 +1,67 @@
|
||||
<?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,
|
||||
]));
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user