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 $query |
||
109 | * |
||
110 | * @return \Illuminate\Database\Eloquent\Builder |
||
111 | */ |
||
112 | public function scopePublished(Builder $query) |
||
117 | |||
118 | /** |
||
119 | * Scope only published posts. |
||
120 | * |
||
121 | * @param \Illuminate\Database\Eloquent\Builder $query |
||
122 | * @param int $year |
||
123 | * |
||
124 | * @return \Illuminate\Database\Eloquent\Builder |
||
125 | */ |
||
126 | public function scopePublishedAt(Builder $query, $year) |
||
131 | |||
132 | /** |
||
133 | * Scope by post's locale. |
||
134 | * |
||
135 | * @param \Illuminate\Database\Eloquent\Builder $query |
||
136 | * @param string|null $locale |
||
137 | * |
||
138 | * @return \Illuminate\Database\Eloquent\Builder |
||
139 | */ |
||
140 | public function scopeLocalized(Builder $query, $locale = null) |
||
144 | |||
145 | /* ----------------------------------------------------------------- |
||
146 | | Relationships |
||
147 | | ----------------------------------------------------------------- |
||
148 | */ |
||
149 | |||
150 | /** |
||
151 | * Author relationship. |
||
152 | * |
||
153 | * @return \Illuminate\Database\Eloquent\Relations\BelongsTo |
||
154 | */ |
||
155 | public function author() |
||
162 | |||
163 | /** |
||
164 | * Category relationship. |
||
165 | * |
||
166 | * @return \Illuminate\Database\Eloquent\Relations\BelongsTo |
||
167 | */ |
||
168 | public function category() |
||
172 | |||
173 | /** |
||
174 | * Tags relationship. |
||
175 | * |
||
176 | * @return \Illuminate\Database\Eloquent\Relations\BelongsToMany |
||
177 | */ |
||
178 | public function tags() |
||
182 | |||
183 | /* ----------------------------------------------------------------- |
||
184 | | Getters & Setters |
||
185 | | ----------------------------------------------------------------- |
||
186 | */ |
||
187 | |||
188 | /** |
||
189 | * Set the title attribute. |
||
190 | * |
||
191 | * @param string $title |
||
192 | */ |
||
193 | public function setTitleAttribute($title) |
||
198 | |||
199 | /** |
||
200 | * Set the content attribute. |
||
201 | * |
||
202 | * @param string $content |
||
203 | */ |
||
204 | public function setContentAttribute($content) |
||
209 | |||
210 | /* ----------------------------------------------------------------- |
||
211 | | Main Functions |
||
212 | | ----------------------------------------------------------------- |
||
213 | */ |
||
214 | |||
215 | /** |
||
216 | * Create a post. |
||
217 | * |
||
218 | * @param array $attributes |
||
219 | * |
||
220 | * @return self |
||
221 | */ |
||
222 | public static function createOne(array $attributes) |
||
235 | |||
236 | /** |
||
237 | * Create a post. |
||
238 | * |
||
239 | * @param array $inputs |
||
240 | * |
||
241 | * @return bool|int |
||
242 | */ |
||
243 | public function updateOne(array $inputs) |
||
256 | |||
257 | /* ----------------------------------------------------------------- |
||
258 | | Check Functions |
||
259 | | ----------------------------------------------------------------- |
||
260 | */ |
||
261 | |||
262 | /** |
||
263 | * Check if the post's status is "draft". |
||
264 | * |
||
265 | * @return bool |
||
266 | */ |
||
267 | public function isDraft() |
||
271 | |||
272 | /** |
||
273 | * Check if the post's status is "published". |
||
274 | * |
||
275 | * @return bool |
||
276 | */ |
||
277 | public function isPublished() |
||
281 | |||
282 | /** |
||
283 | * Check if the post has thumbnail. |
||
284 | * |
||
285 | * @return bool |
||
286 | */ |
||
287 | public function hasThumbnail() |
||
291 | |||
292 | /* ----------------------------------------------------------------- |
||
293 | | Other Methods |
||
294 | | ----------------------------------------------------------------- |
||
295 | */ |
||
296 | |||
297 | /** |
||
298 | * Get the post statuses. |
||
299 | * |
||
300 | * @return \Illuminate\Support\Collection |
||
301 | */ |
||
302 | public static function getStatuses() |
||
308 | |||
309 | /** |
||
310 | * Extract the seo attributes. |
||
311 | * |
||
312 | * @param array $inputs |
||
313 | * |
||
314 | * @return array |
||
315 | */ |
||
316 | protected static function extractSeoAttributes(array $inputs) |
||
325 | |||
326 | /** |
||
327 | * Get the show url. |
||
328 | * |
||
329 | * @return string |
||
330 | */ |
||
331 | public function getShowUrl() |
||
335 | |||
336 | /** |
||
337 | * Get the edit url. |
||
338 | * |
||
339 | * @return string |
||
340 | */ |
||
341 | public function getEditUrl() |
||
345 | } |
||
346 |