49 lines
1.6 KiB
PHP
49 lines
1.6 KiB
PHP
<?php
|
|
|
|
namespace Modules\Blog\Models;
|
|
|
|
use Illuminate\Database\Eloquent\Builder;
|
|
use Illuminate\Database\Eloquent\Model;
|
|
use Illuminate\Database\Eloquent\Relations\BelongsTo;
|
|
use Illuminate\Database\Eloquent\Relations\BelongsToMany;
|
|
use Modules\Authors\Models\Author;
|
|
use Modules\Categories\Models\Category;
|
|
use Modules\Localization\Concerns\HasTranslations;
|
|
use Modules\Sites\Concerns\BelongsToSites;
|
|
use Modules\Tags\Models\Tag;
|
|
use Modules\Workflow\Concerns\HasWorkflow;
|
|
|
|
final class Blog extends Model
|
|
{
|
|
use BelongsToSites, HasTranslations, HasWorkflow;
|
|
|
|
protected $table = 'blog_posts';
|
|
|
|
protected $fillable = ['author_id', 'category_id', 'title', 'slug', 'excerpt', 'content', 'featured_image', 'status', 'is_featured', 'published_at', 'expires_at', 'meta_title', 'meta_description'];
|
|
|
|
protected function casts(): array
|
|
{
|
|
return ['is_featured' => 'boolean', 'published_at' => 'datetime', 'expires_at' => 'datetime'];
|
|
}
|
|
|
|
public function author(): BelongsTo
|
|
{
|
|
return $this->belongsTo(Author::class);
|
|
}
|
|
|
|
public function category(): BelongsTo
|
|
{
|
|
return $this->belongsTo(Category::class);
|
|
}
|
|
|
|
public function tags(): BelongsToMany
|
|
{
|
|
return $this->belongsToMany(Tag::class, 'blog_post_tag');
|
|
}
|
|
|
|
public function scopePublished(Builder $query): Builder
|
|
{
|
|
return $query->where('status', 'published')->where(fn (Builder $q) => $q->whereNull('published_at')->orWhere('published_at', '<=', now()))->where(fn (Builder $q) => $q->whereNull('expires_at')->orWhere('expires_at', '>', now()));
|
|
}
|
|
}
|