Files
ABSDEV/Modules/News/Models/News.php
T
2026-07-24 14:59:44 +01:00

49 lines
1.6 KiB
PHP

<?php
namespace Modules\News\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 News extends Model
{
use BelongsToSites, HasTranslations, HasWorkflow;
protected $table = 'news_articles';
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, 'news_article_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()));
}
}