1 | <?php namespace Arcanesoft\Blog\Models; |
||
39 | class Post extends AbstractModel |
||
40 | { |
||
41 | /* ----------------------------------------------------------------- |
||
42 | | Constants |
||
43 | | ----------------------------------------------------------------- |
||
44 | */ |
||
45 | |||
46 | const STATUS_DRAFT = 'draft'; |
||
47 | const STATUS_PUBLISHED = 'published'; |
||
48 | |||
49 | /* ----------------------------------------------------------------- |
||
50 | | Traits |
||
51 | | ----------------------------------------------------------------- |
||
52 | */ |
||
53 | |||
54 | use Presenters\PostPresenter, |
||
55 | Seoable, |
||
56 | SoftDeletes; |
||
57 | |||
58 | /* ----------------------------------------------------------------- |
||
59 | | Properties |
||
60 | | ----------------------------------------------------------------- |
||
61 | */ |
||
62 | |||
63 | /** |
||
64 | * The database table used by the model |
||
65 | * |
||
66 | * @var string |
||
67 | */ |
||
68 | protected $table = 'posts'; |
||
69 | |||
70 | /** |
||
71 | * The attributes that are mass assignable |
||
72 | * |
||
73 | * @var array |
||
74 | */ |
||
75 | protected $fillable = [ |
||
76 | 'author_id', 'category_id', 'title', 'excerpt', 'content', 'published_at' |
||
77 | ]; |
||
78 | |||
79 | /** |
||
80 | * The attributes that should be mutated to dates. |
||
81 | * |
||
82 | * @var array |
||
83 | */ |
||
84 | protected $dates = ['published_at', 'deleted_at']; |
||
85 | |||
86 | /** |
||
87 | * The attributes that should be casted to native types. |
||
88 | * |
||
89 | * @var array |
||
90 | */ |
||
91 | protected $casts = [ |
||
92 | 'author_id' => 'integer', |
||
93 | 'category_id' => 'integer', |
||
94 | 'is_draft' => 'boolean', |
||
95 | ]; |
||
96 | |||
97 | /* ----------------------------------------------------------------- |
||
98 | | Scopes |
||
99 | | ----------------------------------------------------------------- |
||
100 | */ |
||
101 | |||
102 | /** |
||
103 | * Scope only published posts. |
||
104 | * |
||
105 | * @param \Illuminate\Database\Eloquent\Builder $builder |
||
106 | */ |
||
107 | public function scopePublished(Builder $builder) |
||
112 | |||
113 | /** |
||
114 | * Scope only published posts. |
||
115 | * |
||
116 | * @param \Illuminate\Database\Eloquent\Builder $builder |
||
117 | * @param int $year |
||
118 | */ |
||
119 | public function scopePublishedAt(Builder $builder, $year) |
||
124 | |||
125 | /* ----------------------------------------------------------------- |
||
126 | | Relationships |
||
127 | | ----------------------------------------------------------------- |
||
128 | */ |
||
129 | |||
130 | /** |
||
131 | * Author relationship. |
||
132 | * |
||
133 | * @return \Illuminate\Database\Eloquent\Relations\BelongsTo |
||
134 | */ |
||
135 | public function author() |
||
142 | |||
143 | /** |
||
144 | * Category relationship. |
||
145 | * |
||
146 | * @return \Illuminate\Database\Eloquent\Relations\BelongsTo |
||
147 | */ |
||
148 | public function category() |
||
152 | |||
153 | /** |
||
154 | * Tags relationship. |
||
155 | * |
||
156 | * @return \Illuminate\Database\Eloquent\Relations\BelongsToMany |
||
157 | */ |
||
158 | public function tags() |
||
162 | |||
163 | /* ----------------------------------------------------------------- |
||
164 | | Getters & Setters |
||
165 | | ----------------------------------------------------------------- |
||
166 | */ |
||
167 | |||
168 | /** |
||
169 | * Set the title attribute. |
||
170 | * |
||
171 | * @param string $title |
||
172 | */ |
||
173 | public function setTitleAttribute($title) |
||
178 | |||
179 | /** |
||
180 | * Set the content attribute. |
||
181 | * |
||
182 | * @param string $content |
||
183 | */ |
||
184 | public function setContentAttribute($content) |
||
189 | |||
190 | /** |
||
191 | * Get the content attribute. |
||
192 | * |
||
193 | * @return \Illuminate\Support\HtmlString |
||
194 | */ |
||
195 | public function getContentAttribute() |
||
199 | |||
200 | /** |
||
201 | * Set the status attribute. |
||
202 | * |
||
203 | * @param string $status |
||
204 | */ |
||
205 | public function setStatusAttribute($status) |
||
209 | |||
210 | /** |
||
211 | * Get the status name attribute. |
||
212 | * |
||
213 | * @return string|null |
||
214 | */ |
||
215 | public function getStatusNameAttribute() |
||
221 | |||
222 | /* ----------------------------------------------------------------- |
||
223 | | Main Functions |
||
224 | | ----------------------------------------------------------------- |
||
225 | */ |
||
226 | |||
227 | /** |
||
228 | * Create a post. |
||
229 | * |
||
230 | * @param array $attributes |
||
231 | * |
||
232 | * @return self |
||
233 | */ |
||
234 | public static function createOne(array $attributes) |
||
248 | |||
249 | /** |
||
250 | * Create a post. |
||
251 | * |
||
252 | * @param array $inputs |
||
253 | * |
||
254 | * @return bool|int |
||
255 | */ |
||
256 | public function updateOne(array $inputs) |
||
268 | |||
269 | /* ----------------------------------------------------------------- |
||
270 | | Check Functions |
||
271 | | ----------------------------------------------------------------- |
||
272 | */ |
||
273 | |||
274 | /** |
||
275 | * Check if the post's status is "draft". |
||
276 | * |
||
277 | * @return bool |
||
278 | */ |
||
279 | public function isDraft() |
||
283 | |||
284 | /** |
||
285 | * Check if the post's status is "published". |
||
286 | * |
||
287 | * @return bool |
||
288 | */ |
||
289 | public function isPublished() |
||
293 | |||
294 | /* ----------------------------------------------------------------- |
||
295 | | Other Methods |
||
296 | | ----------------------------------------------------------------- |
||
297 | */ |
||
298 | |||
299 | /** |
||
300 | * Get the post statuses. |
||
301 | * |
||
302 | * @return \Illuminate\Support\Collection |
||
303 | */ |
||
304 | public static function getStatuses() |
||
310 | |||
311 | /** |
||
312 | * Extract the seo attributes. |
||
313 | * |
||
314 | * @param array $inputs |
||
315 | * |
||
316 | * @return array |
||
317 | */ |
||
318 | protected static function extractSeoAttributes(array $inputs) |
||
327 | |||
328 | /** |
||
329 | * Get the show url. |
||
330 | * |
||
331 | * @return string |
||
332 | */ |
||
333 | public function getShowUrl() |
||
337 | |||
338 | /** |
||
339 | * Get the edit url. |
||
340 | * |
||
341 | * @return string |
||
342 | */ |
||
343 | public function getEditUrl() |
||
347 | } |
||
348 |