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', |
||
72 | 'category_id', |
||
73 | 'locale', |
||
74 | 'title', |
||
75 | 'slug', |
||
76 | 'excerpt', |
||
77 | 'thumbnail', |
||
78 | 'content', |
||
79 | 'published_at', |
||
80 | 'status', |
||
81 | ]; |
||
82 | |||
83 | /** |
||
84 | * The attributes that should be mutated to dates. |
||
85 | * |
||
86 | * @var array |
||
87 | */ |
||
88 | protected $dates = ['published_at', 'deleted_at']; |
||
89 | |||
90 | /** |
||
91 | * The attributes that should be casted to native types. |
||
92 | * |
||
93 | * @var array |
||
94 | */ |
||
95 | protected $casts = [ |
||
96 | 'author_id' => 'integer', |
||
97 | 'category_id' => 'integer', |
||
98 | 'is_draft' => 'boolean', |
||
99 | ]; |
||
100 | |||
101 | /** |
||
102 | * The event map for the model. |
||
103 | * |
||
104 | * @var array |
||
105 | */ |
||
106 | protected $dispatchesEvents = [ |
||
107 | 'creating' => PostEvents\PostCreating::class, |
||
108 | 'created' => PostEvents\PostCreated::class, |
||
109 | 'updating' => PostEvents\PostUpdating::class, |
||
110 | 'updated' => PostEvents\PostUpdated::class, |
||
111 | 'saving' => PostEvents\PostSaving::class, |
||
112 | 'saved' => PostEvents\PostSaved::class, |
||
113 | 'deleting' => PostEvents\PostDeleting::class, |
||
114 | 'deleted' => PostEvents\PostDeleted::class, |
||
115 | 'restoring' => PostEvents\PostRestoring::class, |
||
116 | 'restored' => PostEvents\PostRestored::class, |
||
117 | ]; |
||
118 | |||
119 | /* ----------------------------------------------------------------- |
||
120 | | Scopes |
||
121 | | ----------------------------------------------------------------- |
||
122 | */ |
||
123 | |||
124 | /** |
||
125 | * Scope only published posts. |
||
126 | * |
||
127 | * @param \Illuminate\Database\Eloquent\Builder $query |
||
128 | * |
||
129 | * @return \Illuminate\Database\Eloquent\Builder |
||
130 | */ |
||
131 | public function scopePublished(Builder $query) |
||
136 | |||
137 | /** |
||
138 | * Scope only published posts. |
||
139 | * |
||
140 | * @param \Illuminate\Database\Eloquent\Builder $query |
||
141 | * @param int $year |
||
142 | * |
||
143 | * @return \Illuminate\Database\Eloquent\Builder |
||
144 | */ |
||
145 | public function scopePublishedAt(Builder $query, $year) |
||
150 | |||
151 | /** |
||
152 | * Scope by post's locale. |
||
153 | * |
||
154 | * @param \Illuminate\Database\Eloquent\Builder $query |
||
155 | * @param string|null $locale |
||
156 | * |
||
157 | * @return \Illuminate\Database\Eloquent\Builder |
||
158 | */ |
||
159 | 3 | public function scopeLocalized(Builder $query, $locale = null) |
|
163 | |||
164 | /* ----------------------------------------------------------------- |
||
165 | | Relationships |
||
166 | | ----------------------------------------------------------------- |
||
167 | */ |
||
168 | |||
169 | /** |
||
170 | * Author relationship. |
||
171 | * |
||
172 | * @return \Illuminate\Database\Eloquent\Relations\BelongsTo |
||
173 | */ |
||
174 | 3 | public function author() |
|
181 | |||
182 | /** |
||
183 | * Category relationship. |
||
184 | * |
||
185 | * @return \Illuminate\Database\Eloquent\Relations\BelongsTo |
||
186 | */ |
||
187 | 3 | public function category() |
|
191 | |||
192 | /** |
||
193 | * Tags relationship. |
||
194 | * |
||
195 | * @return \Illuminate\Database\Eloquent\Relations\BelongsToMany |
||
196 | */ |
||
197 | 6 | public function tags() |
|
201 | |||
202 | /* ----------------------------------------------------------------- |
||
203 | | Getters & Setters |
||
204 | | ----------------------------------------------------------------- |
||
205 | */ |
||
206 | |||
207 | /** |
||
208 | * Set the title attribute. |
||
209 | * |
||
210 | * @param string $title |
||
211 | */ |
||
212 | 15 | public function setTitleAttribute($title) |
|
216 | |||
217 | /** |
||
218 | * Get the slug attribute. |
||
219 | * |
||
220 | * @param string $slug |
||
221 | */ |
||
222 | 15 | public function setSlugAttribute($slug) |
|
226 | |||
227 | /** |
||
228 | * Set the content attribute. |
||
229 | * |
||
230 | * @param string $content |
||
231 | */ |
||
232 | 15 | public function setContentAttribute($content) |
|
237 | |||
238 | /* ----------------------------------------------------------------- |
||
239 | | Main Functions |
||
240 | | ----------------------------------------------------------------- |
||
241 | */ |
||
242 | |||
243 | /** |
||
244 | * Create a post. |
||
245 | * |
||
246 | * @param array $attributes |
||
247 | * |
||
248 | * @return self |
||
249 | */ |
||
250 | public static function createOne(array $attributes) |
||
264 | |||
265 | /** |
||
266 | * Create a post. |
||
267 | * |
||
268 | * @param array $attributes |
||
269 | * |
||
270 | * @return bool|int |
||
271 | */ |
||
272 | 3 | public function updateOne(array $attributes) |
|
288 | |||
289 | /* ----------------------------------------------------------------- |
||
290 | | Check Functions |
||
291 | | ----------------------------------------------------------------- |
||
292 | */ |
||
293 | |||
294 | /** |
||
295 | * Check if the post is deletable. |
||
296 | * |
||
297 | * @return bool |
||
298 | */ |
||
299 | public function isDeletable() |
||
303 | |||
304 | /** |
||
305 | * Check if the post's status is "draft". |
||
306 | * |
||
307 | * @return bool |
||
308 | */ |
||
309 | 3 | public function isDraft() |
|
315 | |||
316 | /** |
||
317 | * Check if the post's status is "published". |
||
318 | * |
||
319 | * @return bool |
||
320 | */ |
||
321 | 3 | public function isPublished() |
|
325 | |||
326 | /** |
||
327 | * Check if the post has thumbnail. |
||
328 | * |
||
329 | * @return bool |
||
330 | */ |
||
331 | 3 | public function hasThumbnail() |
|
335 | |||
336 | /* ----------------------------------------------------------------- |
||
337 | | Other Methods |
||
338 | | ----------------------------------------------------------------- |
||
339 | */ |
||
340 | |||
341 | /** |
||
342 | * Extract the seo attributes. |
||
343 | * |
||
344 | * @param array $inputs |
||
345 | * |
||
346 | * @return array |
||
347 | */ |
||
348 | protected static function extractSeoAttributes(array $inputs) |
||
357 | } |
||
358 |