2026-04-03 05:18:34 +07:00
|
|
|
<?php
|
|
|
|
|
|
|
|
|
|
namespace App\Models;
|
|
|
|
|
|
|
|
|
|
use Illuminate\Database\Eloquent\Model;
|
|
|
|
|
use Illuminate\Database\Eloquent\Relations\BelongsTo;
|
|
|
|
|
use Illuminate\Support\Str;
|
|
|
|
|
|
|
|
|
|
class Post extends Model
|
|
|
|
|
{
|
2026-04-03 06:48:06 +07:00
|
|
|
protected $fillable = ['title', 'slug', 'category', 'content', 'author_id', 'published_at', 'status', 'reviewed_by', 'rejection_reason'];
|
2026-04-03 05:18:34 +07:00
|
|
|
|
|
|
|
|
protected $casts = ['published_at' => 'datetime'];
|
|
|
|
|
|
|
|
|
|
protected static function booted(): void
|
|
|
|
|
{
|
|
|
|
|
static::creating(function (Post $post) {
|
|
|
|
|
$post->slug ??= Str::slug($post->title);
|
2026-04-03 06:48:06 +07:00
|
|
|
$post->author_id ??= auth()->id();
|
2026-04-03 05:18:34 +07:00
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public function author(): BelongsTo
|
|
|
|
|
{
|
|
|
|
|
return $this->belongsTo(User::class, 'author_id');
|
|
|
|
|
}
|
|
|
|
|
|
2026-04-03 06:48:06 +07:00
|
|
|
public function reviewer(): BelongsTo
|
|
|
|
|
{
|
|
|
|
|
return $this->belongsTo(User::class, 'reviewed_by');
|
|
|
|
|
}
|
|
|
|
|
|
2026-04-03 05:18:34 +07:00
|
|
|
public function scopePublished($query)
|
|
|
|
|
{
|
2026-04-03 06:48:06 +07:00
|
|
|
return $query->where('status', 'published')
|
|
|
|
|
->whereNotNull('published_at')
|
|
|
|
|
->where('published_at', '<=', now());
|
2026-04-03 05:18:34 +07:00
|
|
|
}
|
|
|
|
|
}
|