1 | <?php |
||
2 | |||
3 | declare(strict_types=1); |
||
4 | |||
5 | namespace Xetaravel\Models; |
||
6 | |||
7 | use Eloquence\Behaviours\CountCache\CountedBy; |
||
8 | use Eloquence\Behaviours\CountCache\HasCounts; |
||
9 | use Eloquence\Behaviours\HasSlugs; |
||
10 | use Illuminate\Database\Eloquent\Attributes\ObservedBy; |
||
11 | use Illuminate\Database\Eloquent\Relations\BelongsTo; |
||
12 | use Illuminate\Database\Eloquent\Relations\HasMany; |
||
13 | use Spatie\Image\Enums\Fit; |
||
14 | use Spatie\MediaLibrary\HasMedia; |
||
15 | use Spatie\MediaLibrary\InteractsWithMedia; |
||
16 | use Spatie\MediaLibrary\MediaCollections\Models\Media; |
||
17 | use Xetaio\Mentions\Models\Traits\HasMentionsTrait; |
||
18 | use Xetaravel\Models\Presenters\BlogArticlePresenter; |
||
19 | use Xetaravel\Observers\BlogArticleObserver; |
||
20 | |||
21 | #[ObservedBy([BlogArticleObserver::class])] |
||
22 | class BlogArticle extends Model implements HasMedia |
||
23 | { |
||
24 | use BlogArticlePresenter; |
||
0 ignored issues
–
show
introduced
by
![]() |
|||
25 | use HasCounts; |
||
26 | use HasMentionsTrait; |
||
27 | use HasSlugs; |
||
28 | use InteractsWithMedia; |
||
0 ignored issues
–
show
|
|||
29 | |||
30 | /** |
||
31 | * The attributes that are mass assignable. |
||
32 | * |
||
33 | * @var array |
||
34 | */ |
||
35 | protected $fillable = [ |
||
36 | 'title', |
||
37 | 'slug', |
||
38 | 'user_id', |
||
39 | 'blog_category_id', |
||
40 | 'published_at', |
||
41 | 'content' |
||
42 | ]; |
||
43 | |||
44 | /** |
||
45 | * The accessors to append to the model's array form. |
||
46 | * |
||
47 | * @var array |
||
48 | */ |
||
49 | protected $appends = [ |
||
50 | 'show_url', |
||
51 | 'is_display', |
||
52 | |||
53 | // Media Model |
||
54 | 'article_banner' |
||
55 | ]; |
||
56 | |||
57 | /** |
||
58 | * The attributes that should be cast. |
||
59 | */ |
||
60 | protected function casts(): array |
||
61 | { |
||
62 | return [ |
||
63 | 'published_at' => 'datetime', |
||
64 | 'is_display' => 'boolean', |
||
65 | ]; |
||
66 | } |
||
67 | |||
68 | /** |
||
69 | * Return the field to slug. |
||
70 | * |
||
71 | * @return string |
||
72 | */ |
||
73 | public function slugStrategy(): string |
||
74 | { |
||
75 | return 'title'; |
||
76 | } |
||
77 | |||
78 | /** |
||
79 | * Register the related to the Model. |
||
80 | * |
||
81 | * @param Media|null $media |
||
82 | * |
||
83 | * @return void |
||
84 | */ |
||
85 | public function registerMediaConversions(Media $media = null): void |
||
86 | { |
||
87 | $this->addMediaConversion('article.banner') |
||
88 | ->fit(Fit::Contain, 870, 350) |
||
89 | ->keepOriginalImageFormat() |
||
90 | ->nonQueued(); |
||
91 | } |
||
92 | |||
93 | /** |
||
94 | * Get the category that owns the article. |
||
95 | * |
||
96 | * @return BelongsTo |
||
97 | */ |
||
98 | #[CountedBy(as: 'blog_article_count')] |
||
99 | public function category(): BelongsTo |
||
100 | { |
||
101 | return $this->belongsTo(BlogCategory::class, 'blog_category_id'); |
||
102 | } |
||
103 | |||
104 | /** |
||
105 | * Get the user that owns the article. |
||
106 | * |
||
107 | * @return BelongsTo |
||
108 | */ |
||
109 | #[CountedBy(as: 'blog_article_count')] |
||
110 | public function user(): BelongsTo |
||
111 | { |
||
112 | return $this->belongsTo(User::class)->withTrashed(); |
||
113 | } |
||
114 | |||
115 | /** |
||
116 | * Get the comments for the article. |
||
117 | * |
||
118 | * @return HasMany |
||
119 | */ |
||
120 | public function comments(): HasMany |
||
121 | { |
||
122 | return $this->hasMany(BlogComment::class); |
||
123 | } |
||
124 | } |
||
125 |