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', 'excerpt', 'thumbnail', 'content', 'published_at', |
||
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 | public function scopeLocalized(Builder $query, $locale = null) |
||
154 | |||
155 | /* ----------------------------------------------------------------- |
||
156 | | Relationships |
||
157 | | ----------------------------------------------------------------- |
||
158 | */ |
||
159 | |||
160 | /** |
||
161 | * Author relationship. |
||
162 | * |
||
163 | * @return \Illuminate\Database\Eloquent\Relations\BelongsTo |
||
164 | */ |
||
165 | public function author() |
||
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 | 2 | public function tags() |
|
192 | |||
193 | /* ----------------------------------------------------------------- |
||
194 | | Getters & Setters |
||
195 | | ----------------------------------------------------------------- |
||
196 | */ |
||
197 | |||
198 | /** |
||
199 | * Set the title attribute. |
||
200 | * |
||
201 | * @param string $title |
||
202 | */ |
||
203 | 8 | public function setTitleAttribute($title) |
|
208 | |||
209 | /** |
||
210 | * Set the content attribute. |
||
211 | * |
||
212 | * @param string $content |
||
213 | */ |
||
214 | 8 | public function setContentAttribute($content) |
|
219 | |||
220 | /* ----------------------------------------------------------------- |
||
221 | | Main Functions |
||
222 | | ----------------------------------------------------------------- |
||
223 | */ |
||
224 | |||
225 | /** |
||
226 | * Create a post. |
||
227 | * |
||
228 | * @param array $attributes |
||
229 | * |
||
230 | * @return self |
||
231 | */ |
||
232 | public static function createOne(array $attributes) |
||
244 | |||
245 | /** |
||
246 | * Create a post. |
||
247 | * |
||
248 | * @param array $inputs |
||
249 | * |
||
250 | * @return bool|int |
||
251 | */ |
||
252 | public function updateOne(array $inputs) |
||
265 | |||
266 | /* ----------------------------------------------------------------- |
||
267 | | Check Functions |
||
268 | | ----------------------------------------------------------------- |
||
269 | */ |
||
270 | |||
271 | /** |
||
272 | * Check if the post's status is "draft". |
||
273 | * |
||
274 | * @return bool |
||
275 | */ |
||
276 | public function isDraft() |
||
280 | |||
281 | /** |
||
282 | * Check if the post's status is "published". |
||
283 | * |
||
284 | * @return bool |
||
285 | */ |
||
286 | public function isPublished() |
||
290 | |||
291 | /** |
||
292 | * Check if the post has thumbnail. |
||
293 | * |
||
294 | * @return bool |
||
295 | */ |
||
296 | public function hasThumbnail() |
||
300 | |||
301 | /* ----------------------------------------------------------------- |
||
302 | | Other Methods |
||
303 | | ----------------------------------------------------------------- |
||
304 | */ |
||
305 | |||
306 | /** |
||
307 | * Extract the seo attributes. |
||
308 | * |
||
309 | * @param array $inputs |
||
310 | * |
||
311 | * @return array |
||
312 | */ |
||
313 | 2 | protected static function extractSeoAttributes(array $inputs) |
|
322 | |||
323 | /** |
||
324 | * Get the show url. |
||
325 | * |
||
326 | * @return string |
||
327 | */ |
||
328 | public function getShowUrl() |
||
332 | |||
333 | /** |
||
334 | * Get the edit url. |
||
335 | * |
||
336 | * @return string |
||
337 | */ |
||
338 | public function getEditUrl() |
||
342 | } |
||
343 |