1 | <?php namespace Arcanesoft\Blog\Models; |
||
42 | class Post extends AbstractModel |
||
43 | { |
||
44 | /* ----------------------------------------------------------------- |
||
45 | | Constants |
||
46 | | ----------------------------------------------------------------- |
||
47 | */ |
||
48 | |||
49 | const STATUS_DRAFT = 'draft'; |
||
50 | const STATUS_PUBLISHED = 'published'; |
||
51 | |||
52 | /* ----------------------------------------------------------------- |
||
53 | | Traits |
||
54 | | ----------------------------------------------------------------- |
||
55 | */ |
||
56 | |||
57 | use Presenters\PostPresenter, |
||
58 | Seoable, |
||
59 | SoftDeletes; |
||
60 | |||
61 | /* ----------------------------------------------------------------- |
||
62 | | Properties |
||
63 | | ----------------------------------------------------------------- |
||
64 | */ |
||
65 | |||
66 | /** |
||
67 | * The database table used by the model |
||
68 | * |
||
69 | * @var string |
||
70 | */ |
||
71 | protected $table = 'posts'; |
||
72 | |||
73 | /** |
||
74 | * The attributes that are mass assignable |
||
75 | * |
||
76 | * @var array |
||
77 | */ |
||
78 | protected $fillable = [ |
||
79 | 'author_id', 'category_id', 'locale', 'title', 'excerpt', 'thumbnail', 'content', 'published_at', |
||
80 | ]; |
||
81 | |||
82 | /** |
||
83 | * The attributes that should be mutated to dates. |
||
84 | * |
||
85 | * @var array |
||
86 | */ |
||
87 | protected $dates = ['published_at', 'deleted_at']; |
||
88 | |||
89 | /** |
||
90 | * The attributes that should be casted to native types. |
||
91 | * |
||
92 | * @var array |
||
93 | */ |
||
94 | protected $casts = [ |
||
95 | 'author_id' => 'integer', |
||
96 | 'category_id' => 'integer', |
||
97 | 'is_draft' => 'boolean', |
||
98 | ]; |
||
99 | |||
100 | /* ----------------------------------------------------------------- |
||
101 | | Scopes |
||
102 | | ----------------------------------------------------------------- |
||
103 | */ |
||
104 | |||
105 | /** |
||
106 | * Scope only published posts. |
||
107 | * |
||
108 | * @param \Illuminate\Database\Eloquent\Builder $builder |
||
109 | */ |
||
110 | public function scopePublished(Builder $builder) |
||
115 | |||
116 | /** |
||
117 | * Scope only published posts. |
||
118 | * |
||
119 | * @param \Illuminate\Database\Eloquent\Builder $builder |
||
120 | * @param int $year |
||
121 | */ |
||
122 | public function scopePublishedAt(Builder $builder, $year) |
||
127 | |||
128 | /** |
||
129 | * Scope by post's locale. |
||
130 | * |
||
131 | * @param \Illuminate\Database\Eloquent\Builder $builder |
||
132 | * @param string|null $locale |
||
133 | */ |
||
134 | public function scopeLocalized(Builder $builder, $locale = null) |
||
138 | |||
139 | /* ----------------------------------------------------------------- |
||
140 | | Relationships |
||
141 | | ----------------------------------------------------------------- |
||
142 | */ |
||
143 | |||
144 | /** |
||
145 | * Author relationship. |
||
146 | * |
||
147 | * @return \Illuminate\Database\Eloquent\Relations\BelongsTo |
||
148 | */ |
||
149 | public function author() |
||
156 | |||
157 | /** |
||
158 | * Category relationship. |
||
159 | * |
||
160 | * @return \Illuminate\Database\Eloquent\Relations\BelongsTo |
||
161 | */ |
||
162 | public function category() |
||
166 | |||
167 | /** |
||
168 | * Tags relationship. |
||
169 | * |
||
170 | * @return \Illuminate\Database\Eloquent\Relations\BelongsToMany |
||
171 | */ |
||
172 | public function tags() |
||
176 | |||
177 | /* ----------------------------------------------------------------- |
||
178 | | Getters & Setters |
||
179 | | ----------------------------------------------------------------- |
||
180 | */ |
||
181 | |||
182 | /** |
||
183 | * Set the title attribute. |
||
184 | * |
||
185 | * @param string $title |
||
186 | */ |
||
187 | public function setTitleAttribute($title) |
||
192 | |||
193 | /** |
||
194 | * Set the content attribute. |
||
195 | * |
||
196 | * @param string $content |
||
197 | */ |
||
198 | public function setContentAttribute($content) |
||
203 | |||
204 | /* ----------------------------------------------------------------- |
||
205 | | Main Functions |
||
206 | | ----------------------------------------------------------------- |
||
207 | */ |
||
208 | |||
209 | /** |
||
210 | * Create a post. |
||
211 | * |
||
212 | * @param array $attributes |
||
213 | * |
||
214 | * @return self |
||
215 | */ |
||
216 | public static function createOne(array $attributes) |
||
229 | |||
230 | /** |
||
231 | * Create a post. |
||
232 | * |
||
233 | * @param array $inputs |
||
234 | * |
||
235 | * @return bool|int |
||
236 | */ |
||
237 | public function updateOne(array $inputs) |
||
250 | |||
251 | /* ----------------------------------------------------------------- |
||
252 | | Check Functions |
||
253 | | ----------------------------------------------------------------- |
||
254 | */ |
||
255 | |||
256 | /** |
||
257 | * Check if the post's status is "draft". |
||
258 | * |
||
259 | * @return bool |
||
260 | */ |
||
261 | public function isDraft() |
||
265 | |||
266 | /** |
||
267 | * Check if the post's status is "published". |
||
268 | * |
||
269 | * @return bool |
||
270 | */ |
||
271 | public function isPublished() |
||
275 | |||
276 | /** |
||
277 | * Check if the post has thumbnail. |
||
278 | * |
||
279 | * @return bool |
||
280 | */ |
||
281 | public function hasThumbnail() |
||
285 | |||
286 | /* ----------------------------------------------------------------- |
||
287 | | Other Methods |
||
288 | | ----------------------------------------------------------------- |
||
289 | */ |
||
290 | |||
291 | /** |
||
292 | * Get the post statuses. |
||
293 | * |
||
294 | * @return \Illuminate\Support\Collection |
||
295 | */ |
||
296 | public static function getStatuses() |
||
302 | |||
303 | /** |
||
304 | * Extract the seo attributes. |
||
305 | * |
||
306 | * @param array $inputs |
||
307 | * |
||
308 | * @return array |
||
309 | */ |
||
310 | protected static function extractSeoAttributes(array $inputs) |
||
319 | |||
320 | /** |
||
321 | * Get the show url. |
||
322 | * |
||
323 | * @return string |
||
324 | */ |
||
325 | public function getShowUrl() |
||
329 | |||
330 | /** |
||
331 | * Get the edit url. |
||
332 | * |
||
333 | * @return string |
||
334 | */ |
||
335 | public function getEditUrl() |
||
339 | } |
||
340 |