1
|
|
|
<?php |
2
|
|
|
namespace Xetaravel\Models; |
3
|
|
|
|
4
|
|
|
use Eloquence\Behaviours\CountCache\Countable; |
5
|
|
|
use Eloquence\Behaviours\Sluggable; |
6
|
|
|
use Illuminate\Support\Facades\Auth; |
7
|
|
|
use Spatie\MediaLibrary\HasMedia; |
8
|
|
|
use Spatie\MediaLibrary\InteractsWithMedia; |
9
|
|
|
use Spatie\MediaLibrary\MediaCollections\Models\Media; |
10
|
|
|
use Xetaio\Mentions\Models\Traits\HasMentionsTrait; |
11
|
|
|
use Xetaravel\Models\Category; |
12
|
|
|
use Xetaravel\Models\Presenters\ArticlePresenter; |
13
|
|
|
use Xetaravel\Models\User; |
14
|
|
|
|
15
|
|
|
class Article extends Model implements HasMedia |
16
|
|
|
{ |
17
|
|
|
use Countable, |
|
|
|
|
18
|
|
|
Sluggable, |
19
|
|
|
ArticlePresenter, |
20
|
|
|
HasMentionsTrait, |
21
|
|
|
InteractsWithMedia; |
22
|
|
|
|
23
|
|
|
/** |
24
|
|
|
* The attributes that are mass assignable. |
25
|
|
|
* |
26
|
|
|
* @var array |
27
|
|
|
*/ |
28
|
|
|
protected $fillable = [ |
29
|
|
|
'title', |
30
|
|
|
'user_id', |
31
|
|
|
'category_id', |
32
|
|
|
'is_display', |
33
|
|
|
'content' |
34
|
|
|
]; |
35
|
|
|
|
36
|
|
|
/** |
37
|
|
|
* The accessors to append to the model's array form. |
38
|
|
|
* |
39
|
|
|
* @var array |
40
|
|
|
*/ |
41
|
|
|
protected $appends = [ |
42
|
|
|
'article_url', |
43
|
|
|
|
44
|
|
|
// Media Model |
45
|
|
|
'article_banner' |
46
|
|
|
]; |
47
|
|
|
|
48
|
|
|
/** |
49
|
|
|
* The "booting" method of the model. |
50
|
|
|
* |
51
|
|
|
* @return void |
52
|
|
|
*/ |
53
|
|
|
protected static function boot() |
54
|
|
|
{ |
55
|
|
|
parent::boot(); |
56
|
|
|
|
57
|
|
|
// Generated the slug before updating. |
58
|
|
|
static::updating(function ($model) { |
59
|
|
|
$model->generateSlug(); |
60
|
|
|
}); |
61
|
|
|
|
62
|
|
|
// Set the user id to the new article before saving it. |
63
|
|
|
static::creating(function ($model) { |
64
|
|
|
$model->user_id = Auth::id(); |
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
|
|
|
* Return the count cache configuration. |
80
|
|
|
* |
81
|
|
|
* @return array |
82
|
|
|
*/ |
83
|
|
|
public function countCaches(): array |
84
|
|
|
{ |
85
|
|
|
return [ |
86
|
|
|
User::class, |
87
|
|
|
Category::class |
88
|
|
|
]; |
89
|
|
|
} |
90
|
|
|
|
91
|
|
|
/** |
92
|
|
|
* Register the related to the Model. |
93
|
|
|
* |
94
|
|
|
* @return void |
95
|
|
|
*/ |
96
|
|
|
public function registerMediaConversions(Media $media = null): void |
97
|
|
|
{ |
98
|
|
|
$this->addMediaConversion('article.banner') |
99
|
|
|
->width(825) |
100
|
|
|
->height(250) |
101
|
|
|
->keepOriginalImageFormat(); |
102
|
|
|
|
103
|
|
|
$this->addMediaConversion('original') |
104
|
|
|
->keepOriginalImageFormat(); |
105
|
|
|
} |
106
|
|
|
|
107
|
|
|
/** |
108
|
|
|
* Get the category that owns the article. |
109
|
|
|
* |
110
|
|
|
* @return \Illuminate\Database\Eloquent\Relations\BelongsTo |
111
|
|
|
*/ |
112
|
|
|
public function category() |
113
|
|
|
{ |
114
|
|
|
return $this->belongsTo(Category::class); |
115
|
|
|
} |
116
|
|
|
|
117
|
|
|
/** |
118
|
|
|
* Get the user that owns the article. |
119
|
|
|
* |
120
|
|
|
* @return \Illuminate\Database\Eloquent\Relations\BelongsTo |
121
|
|
|
*/ |
122
|
|
|
public function user() |
123
|
|
|
{ |
124
|
|
|
return $this->belongsTo(User::class); |
125
|
|
|
} |
126
|
|
|
|
127
|
|
|
/** |
128
|
|
|
* Get the comments for the article. |
129
|
|
|
* |
130
|
|
|
* @return \Illuminate\Database\Eloquent\Relations\HasMany |
131
|
|
|
*/ |
132
|
|
|
public function comments() |
133
|
|
|
{ |
134
|
|
|
return $this->hasMany(Comment::class); |
135
|
|
|
} |
136
|
|
|
} |
137
|
|
|
|