1 | <?php namespace Arcanesoft\Blog\Models; |
||
42 | class Post extends AbstractModel |
||
43 | { |
||
44 | /* ----------------------------------------------------------------- |
||
45 | | Traits |
||
46 | | ----------------------------------------------------------------- |
||
47 | */ |
||
48 | |||
49 | use Presenters\PostPresenter, |
||
50 | Seoable, |
||
51 | SoftDeletes; |
||
52 | |||
53 | /* ----------------------------------------------------------------- |
||
54 | | Properties |
||
55 | | ----------------------------------------------------------------- |
||
56 | */ |
||
57 | |||
58 | /** |
||
59 | * The database table used by the model |
||
60 | * |
||
61 | * @var string |
||
62 | */ |
||
63 | protected $table = 'posts'; |
||
64 | |||
65 | /** |
||
66 | * The attributes that are mass assignable |
||
67 | * |
||
68 | * @var array |
||
69 | */ |
||
70 | protected $fillable = [ |
||
71 | 'author_id', 'category_id', 'locale', 'title', 'slug', 'excerpt', 'thumbnail', 'content', 'published_at', 'status', |
||
72 | ]; |
||
73 | |||
74 | /** |
||
75 | * The attributes that should be mutated to dates. |
||
76 | * |
||
77 | * @var array |
||
78 | */ |
||
79 | protected $dates = ['published_at', 'deleted_at']; |
||
80 | |||
81 | /** |
||
82 | * The attributes that should be casted to native types. |
||
83 | * |
||
84 | * @var array |
||
85 | */ |
||
86 | protected $casts = [ |
||
87 | 'author_id' => 'integer', |
||
88 | 'category_id' => 'integer', |
||
89 | 'is_draft' => 'boolean', |
||
90 | ]; |
||
91 | |||
92 | /** |
||
93 | * The event map for the model. |
||
94 | * |
||
95 | * @var array |
||
96 | */ |
||
97 | protected $dispatchesEvents = [ |
||
98 | 'creating' => PostEvents\PostCreating::class, |
||
99 | 'created' => PostEvents\PostCreated::class, |
||
100 | 'updating' => PostEvents\PostUpdating::class, |
||
101 | 'updated' => PostEvents\PostUpdated::class, |
||
102 | 'saving' => PostEvents\PostSaving::class, |
||
103 | 'saved' => PostEvents\PostSaved::class, |
||
104 | 'deleting' => PostEvents\PostDeleting::class, |
||
105 | 'deleted' => PostEvents\PostDeleted::class, |
||
106 | 'restoring' => PostEvents\PostRestoring::class, |
||
107 | 'restored' => PostEvents\PostRestored::class, |
||
108 | ]; |
||
109 | |||
110 | /* ----------------------------------------------------------------- |
||
111 | | Scopes |
||
112 | | ----------------------------------------------------------------- |
||
113 | */ |
||
114 | |||
115 | /** |
||
116 | * Scope only published posts. |
||
117 | * |
||
118 | * @param \Illuminate\Database\Eloquent\Builder $query |
||
119 | * |
||
120 | * @return \Illuminate\Database\Eloquent\Builder |
||
121 | */ |
||
122 | public function scopePublished(Builder $query) |
||
127 | |||
128 | /** |
||
129 | * Scope only published posts. |
||
130 | * |
||
131 | * @param \Illuminate\Database\Eloquent\Builder $query |
||
132 | * @param int $year |
||
133 | * |
||
134 | * @return \Illuminate\Database\Eloquent\Builder |
||
135 | */ |
||
136 | public function scopePublishedAt(Builder $query, $year) |
||
141 | |||
142 | /** |
||
143 | * Scope by post's locale. |
||
144 | * |
||
145 | * @param \Illuminate\Database\Eloquent\Builder $query |
||
146 | * @param string|null $locale |
||
147 | * |
||
148 | * @return \Illuminate\Database\Eloquent\Builder |
||
149 | */ |
||
150 | 2 | public function scopeLocalized(Builder $query, $locale = null) |
|
151 | { |
||
152 | 2 | return $query->where('locale', $locale ?: config('app.locale')); |
|
153 | } |
||
154 | |||
155 | /* ----------------------------------------------------------------- |
||
156 | | Relationships |
||
157 | | ----------------------------------------------------------------- |
||
158 | */ |
||
159 | |||
160 | /** |
||
161 | * Author relationship. |
||
162 | * |
||
163 | * @return \Illuminate\Database\Eloquent\Relations\BelongsTo |
||
164 | */ |
||
165 | 2 | public function author() |
|
166 | { |
||
167 | 2 | return $this->belongsTo( |
|
168 | 2 | config('auth.providers.users.model', 'App\Models\User'), |
|
169 | 2 | 'author_id' |
|
170 | ); |
||
171 | } |
||
172 | |||
173 | /** |
||
174 | * Category relationship. |
||
175 | * |
||
176 | * @return \Illuminate\Database\Eloquent\Relations\BelongsTo |
||
177 | */ |
||
178 | 2 | public function category() |
|
182 | |||
183 | /** |
||
184 | * Tags relationship. |
||
185 | * |
||
186 | * @return \Illuminate\Database\Eloquent\Relations\BelongsToMany |
||
187 | */ |
||
188 | 4 | public function tags() |
|
192 | |||
193 | /* ----------------------------------------------------------------- |
||
194 | | Getters & Setters |
||
195 | | ----------------------------------------------------------------- |
||
196 | */ |
||
197 | |||
198 | /** |
||
199 | * Set the title attribute. |
||
200 | * |
||
201 | * @param string $title |
||
202 | */ |
||
203 | 10 | public function setTitleAttribute($title) |
|
207 | |||
208 | /** |
||
209 | * Get the slug attribute. |
||
210 | * |
||
211 | * @param string $slug |
||
212 | */ |
||
213 | 10 | public function setSlugAttribute($slug) |
|
217 | |||
218 | /** |
||
219 | * Set the content attribute. |
||
220 | * |
||
221 | * @param string $content |
||
222 | */ |
||
223 | 10 | public function setContentAttribute($content) |
|
228 | |||
229 | /* ----------------------------------------------------------------- |
||
230 | | Main Functions |
||
231 | | ----------------------------------------------------------------- |
||
232 | */ |
||
233 | |||
234 | /** |
||
235 | * Create a post. |
||
236 | * |
||
237 | * @param array $attributes |
||
238 | * |
||
239 | * @return self |
||
240 | */ |
||
241 | public static function createOne(array $attributes) |
||
253 | |||
254 | /** |
||
255 | * Create a post. |
||
256 | * |
||
257 | * @param array $attributes |
||
258 | * |
||
259 | * @return bool|int |
||
260 | */ |
||
261 | 2 | public function updateOne(array $attributes) |
|
273 | |||
274 | /* ----------------------------------------------------------------- |
||
275 | | Check Functions |
||
276 | | ----------------------------------------------------------------- |
||
277 | */ |
||
278 | |||
279 | /** |
||
280 | * Check if the post's status is "draft". |
||
281 | * |
||
282 | * @return bool |
||
283 | */ |
||
284 | 2 | public function isDraft() |
|
288 | |||
289 | /** |
||
290 | * Check if the post's status is "published". |
||
291 | * |
||
292 | * @return bool |
||
293 | */ |
||
294 | 2 | public function isPublished() |
|
298 | |||
299 | /** |
||
300 | * Check if the post has thumbnail. |
||
301 | * |
||
302 | * @return bool |
||
303 | */ |
||
304 | 2 | public function hasThumbnail() |
|
308 | |||
309 | /* ----------------------------------------------------------------- |
||
310 | | Other Methods |
||
311 | | ----------------------------------------------------------------- |
||
312 | */ |
||
313 | |||
314 | /** |
||
315 | * Extract the seo attributes. |
||
316 | * |
||
317 | * @param array $inputs |
||
318 | * |
||
319 | * @return array |
||
320 | */ |
||
321 | 2 | protected static function extractSeoAttributes(array $inputs) |
|
330 | } |
||
331 |