1 | <?php namespace Arcanesoft\Blog\Models; |
||
40 | class Post extends AbstractModel |
||
41 | { |
||
42 | /* ----------------------------------------------------------------- |
||
43 | | Constants |
||
44 | | ----------------------------------------------------------------- |
||
45 | */ |
||
46 | |||
47 | const STATUS_DRAFT = 'draft'; |
||
48 | const STATUS_PUBLISHED = 'published'; |
||
49 | |||
50 | /* ----------------------------------------------------------------- |
||
51 | | Traits |
||
52 | | ----------------------------------------------------------------- |
||
53 | */ |
||
54 | |||
55 | use Presenters\PostPresenter, |
||
56 | Seoable, |
||
57 | SoftDeletes; |
||
58 | |||
59 | /* ----------------------------------------------------------------- |
||
60 | | Properties |
||
61 | | ----------------------------------------------------------------- |
||
62 | */ |
||
63 | |||
64 | /** |
||
65 | * The database table used by the model |
||
66 | * |
||
67 | * @var string |
||
68 | */ |
||
69 | protected $table = 'posts'; |
||
70 | |||
71 | /** |
||
72 | * The attributes that are mass assignable |
||
73 | * |
||
74 | * @var array |
||
75 | */ |
||
76 | protected $fillable = [ |
||
77 | 'author_id', 'category_id', 'locale', 'title', 'excerpt', 'thumbnail', 'content', 'published_at', |
||
78 | ]; |
||
79 | |||
80 | /** |
||
81 | * The attributes that should be mutated to dates. |
||
82 | * |
||
83 | * @var array |
||
84 | */ |
||
85 | protected $dates = ['published_at', 'deleted_at']; |
||
86 | |||
87 | /** |
||
88 | * The attributes that should be casted to native types. |
||
89 | * |
||
90 | * @var array |
||
91 | */ |
||
92 | protected $casts = [ |
||
93 | 'author_id' => 'integer', |
||
94 | 'category_id' => 'integer', |
||
95 | 'is_draft' => 'boolean', |
||
96 | ]; |
||
97 | |||
98 | /* ----------------------------------------------------------------- |
||
99 | | Scopes |
||
100 | | ----------------------------------------------------------------- |
||
101 | */ |
||
102 | |||
103 | /** |
||
104 | * Scope only published posts. |
||
105 | * |
||
106 | * @param \Illuminate\Database\Eloquent\Builder $query |
||
107 | * |
||
108 | * @return \Illuminate\Database\Eloquent\Builder |
||
109 | */ |
||
110 | public function scopePublished(Builder $query) |
||
115 | |||
116 | /** |
||
117 | * Scope only published posts. |
||
118 | * |
||
119 | * @param \Illuminate\Database\Eloquent\Builder $query |
||
120 | * @param int $year |
||
121 | * |
||
122 | * @return \Illuminate\Database\Eloquent\Builder |
||
123 | */ |
||
124 | public function scopePublishedAt(Builder $query, $year) |
||
129 | |||
130 | /** |
||
131 | * Scope by post's locale. |
||
132 | * |
||
133 | * @param \Illuminate\Database\Eloquent\Builder $query |
||
134 | * @param string|null $locale |
||
135 | * |
||
136 | * @return \Illuminate\Database\Eloquent\Builder |
||
137 | */ |
||
138 | public function scopeLocalized(Builder $query, $locale = null) |
||
142 | |||
143 | /* ----------------------------------------------------------------- |
||
144 | | Relationships |
||
145 | | ----------------------------------------------------------------- |
||
146 | */ |
||
147 | |||
148 | /** |
||
149 | * Author relationship. |
||
150 | * |
||
151 | * @return \Illuminate\Database\Eloquent\Relations\BelongsTo |
||
152 | */ |
||
153 | public function author() |
||
160 | |||
161 | /** |
||
162 | * Category relationship. |
||
163 | * |
||
164 | * @return \Illuminate\Database\Eloquent\Relations\BelongsTo |
||
165 | */ |
||
166 | public function category() |
||
170 | |||
171 | /** |
||
172 | * Tags relationship. |
||
173 | * |
||
174 | * @return \Illuminate\Database\Eloquent\Relations\BelongsToMany |
||
175 | */ |
||
176 | public function tags() |
||
180 | |||
181 | /* ----------------------------------------------------------------- |
||
182 | | Getters & Setters |
||
183 | | ----------------------------------------------------------------- |
||
184 | */ |
||
185 | |||
186 | /** |
||
187 | * Set the title attribute. |
||
188 | * |
||
189 | * @param string $title |
||
190 | */ |
||
191 | public function setTitleAttribute($title) |
||
196 | |||
197 | /** |
||
198 | * Set the content attribute. |
||
199 | * |
||
200 | * @param string $content |
||
201 | */ |
||
202 | public function setContentAttribute($content) |
||
207 | |||
208 | /* ----------------------------------------------------------------- |
||
209 | | Main Functions |
||
210 | | ----------------------------------------------------------------- |
||
211 | */ |
||
212 | |||
213 | /** |
||
214 | * Create a post. |
||
215 | * |
||
216 | * @param array $attributes |
||
217 | * |
||
218 | * @return self |
||
219 | */ |
||
220 | public static function createOne(array $attributes) |
||
233 | |||
234 | /** |
||
235 | * Create a post. |
||
236 | * |
||
237 | * @param array $inputs |
||
238 | * |
||
239 | * @return bool|int |
||
240 | */ |
||
241 | public function updateOne(array $inputs) |
||
254 | |||
255 | /* ----------------------------------------------------------------- |
||
256 | | Check Functions |
||
257 | | ----------------------------------------------------------------- |
||
258 | */ |
||
259 | |||
260 | /** |
||
261 | * Check if the post's status is "draft". |
||
262 | * |
||
263 | * @return bool |
||
264 | */ |
||
265 | public function isDraft() |
||
269 | |||
270 | /** |
||
271 | * Check if the post's status is "published". |
||
272 | * |
||
273 | * @return bool |
||
274 | */ |
||
275 | public function isPublished() |
||
279 | |||
280 | /** |
||
281 | * Check if the post has thumbnail. |
||
282 | * |
||
283 | * @return bool |
||
284 | */ |
||
285 | public function hasThumbnail() |
||
289 | |||
290 | /* ----------------------------------------------------------------- |
||
291 | | Other Methods |
||
292 | | ----------------------------------------------------------------- |
||
293 | */ |
||
294 | |||
295 | /** |
||
296 | * Extract the seo attributes. |
||
297 | * |
||
298 | * @param array $inputs |
||
299 | * |
||
300 | * @return array |
||
301 | */ |
||
302 | protected static function extractSeoAttributes(array $inputs) |
||
311 | |||
312 | /** |
||
313 | * Get the show url. |
||
314 | * |
||
315 | * @return string |
||
316 | */ |
||
317 | public function getShowUrl() |
||
321 | |||
322 | /** |
||
323 | * Get the edit url. |
||
324 | * |
||
325 | * @return string |
||
326 | */ |
||
327 | public function getEditUrl() |
||
331 | } |
||
332 |