52 lines
1.1 KiB
PHP
52 lines
1.1 KiB
PHP
<?php
|
|||
|
|
|
||
|
|
namespace App\Models;
|
||
|
|
|
||
|
|
use Illuminate\Database\Eloquent\Builder;
|
||
|
|
use Illuminate\Database\Eloquent\Casts\Attribute;
|
||
|
|
use Illuminate\Database\Eloquent\Model;
|
||
|
|
use Illuminate\Support\Facades\Storage;
|
||
|
|
use Illuminate\Support\Str;
|
||
|
|
|
||
|
|
class Post extends Model
|
||
|
|
{
|
||
|
|
protected $fillable = [
|
||
|
|
'title',
|
||
|
|
'slug',
|
||
|
|
'excerpt',
|
||
|
|
'body',
|
||
|
|
'cover_image',
|
||
|
|
'tags',
|
||
|
|
'published_at',
|
||
|
|
];
|
||
|
|
|
||
|
|
protected $casts = [
|
||
|
|
'tags' => 'array',
|
||
|
|
'published_at' => 'datetime',
|
||
|
|
];
|
||
|
|
|
||
|
|
protected static function booted(): void
|
||
|
|
{
|
||
|
|
static::saving(function (Post $post) {
|
||
|
|
if (blank($post->slug)) {
|
||
|
|
$post->slug = Str::slug($post->title);
|
||
|
|
}
|
||
|
|
});
|
||
|
|
}
|
||
|
|
|
||
|
|
public function scopePublished(Builder $query): Builder
|
||
|
|
{
|
||
|
|
return $query->whereNotNull('published_at')->where('published_at', '<=', now());
|
||
|
|
}
|
||
|
|
|
||
|
|
public function getRouteKeyName(): string
|
||
|
|
{
|
||
|
|
return 'slug';
|
||
|
|
}
|
||
|
|
|
||
|
|
protected function coverImageUrl(): Attribute
|
||
|
|
{
|
||
|
|
return Attribute::get(fn () => $this->cover_image ? Storage::disk('public')->url($this->cover_image) : null);
|
||
|
|
}
|
||
|
|
}
|