32 lines
764 B
PHP
32 lines
764 B
PHP
|
|
<?php
|
||
|
|
|
||
|
|
namespace App\Models;
|
||
|
|
|
||
|
|
use Illuminate\Database\Eloquent\Model;
|
||
|
|
use Illuminate\Database\Eloquent\Relations\BelongsTo;
|
||
|
|
use Illuminate\Support\Str;
|
||
|
|
|
||
|
|
class Post extends Model
|
||
|
|
{
|
||
|
|
protected $fillable = ['title', 'slug', 'category', 'content', 'author_id', 'published_at'];
|
||
|
|
|
||
|
|
protected $casts = ['published_at' => 'datetime'];
|
||
|
|
|
||
|
|
protected static function booted(): void
|
||
|
|
{
|
||
|
|
static::creating(function (Post $post) {
|
||
|
|
$post->slug ??= Str::slug($post->title);
|
||
|
|
});
|
||
|
|
}
|
||
|
|
|
||
|
|
public function author(): BelongsTo
|
||
|
|
{
|
||
|
|
return $this->belongsTo(User::class, 'author_id');
|
||
|
|
}
|
||
|
|
|
||
|
|
public function scopePublished($query)
|
||
|
|
{
|
||
|
|
return $query->whereNotNull('published_at')->where('published_at', '<=', now());
|
||
|
|
}
|
||
|
|
}
|