66
77use App \Http \Controllers \PostController ;
88use Carbon \Carbon ;
9- use Exception ;
9+ use Illuminate \ Database \ Eloquent \ Casts \ Attribute ;
1010use Illuminate \Database \Eloquent \Factories \HasFactory ;
1111use Illuminate \Database \Eloquent \Model ;
1212use Illuminate \Database \Eloquent \Relations \BelongsTo ;
1717use Illuminate \Support \Facades \Request ;
1818use Illuminate \Support \Facades \Storage ;
1919use Illuminate \Support \Str ;
20- use InvalidArgumentException ;
2120
2221/**
2322 * Class Post.
@@ -42,6 +41,20 @@ class Post extends Model
4241
4342 protected $ fillable = ['title ' , 'content ' , 'date ' , 'description ' , 'views_count ' ];
4443
44+ protected $ casts = [
45+ 'date ' => 'string ' ,
46+ ];
47+
48+ public function setDateAttribute ($ value ): void
49+ {
50+ if (empty ($ value )) {
51+ $ this ->attributes ['date ' ] = null ;
52+ return ;
53+ }
54+
55+ $ this ->attributes ['date ' ] = Carbon::parse ($ value )->format ('Y-m-d ' );
56+ }
57+
4558 public function category (): BelongsTo
4659 {
4760 return $ this ->belongsTo (Category::class);
@@ -182,40 +195,6 @@ public function toggleFeatured($value): void
182195 $ this ->setFeatured ();
183196 }
184197
185- public function setDateAttribute ($ value ): void
186- {
187- if (empty ($ value )) {
188- $ this ->attributes ['date ' ] = null ;
189- return ;
190- }
191-
192- try {
193- $ date = Carbon::createFromFormat ('Y-m-d ' , $ value );
194- if ($ date !== false ) {
195- $ this ->attributes ['date ' ] = $ date ->format ('Y-m-d ' );
196- return ;
197- }
198- } catch (Exception $ e ) {
199- // Continue to try Carbon::parse
200- }
201-
202- try {
203- $ date = Carbon::parse ($ value );
204- $ this ->attributes ['date ' ] = $ date ->format ('Y-m-d ' );
205- } catch (Exception $ parseException ) {
206- $ this ->attributes ['date ' ] = null ;
207- }
208- }
209-
210- public function getDateAttribute (): ?string
211- {
212- if (isset ($ this ->attributes ['date ' ]) && !empty ($ this ->attributes ['date ' ])) {
213- return $ this ->attributes ['date ' ];
214- }
215-
216- return null ;
217- }
218-
219198 public function getCategoryTitle (): string
220199 {
221200 return $ this ->category ->title ?? 'Нет категории ' ;
@@ -288,7 +267,7 @@ public function getDescription(): string
288267 {
289268 return empty ($ this ->description )
290269 ? 'Справочник по тематике программирования на языках PHP, JS '
291- : strip_tags ($ this ->description );
270+ : strip_tags (( string ) $ this ->description );
292271 }
293272
294273 public function getTitle (): string
@@ -304,15 +283,8 @@ public function getTitle(): string
304283 return $ this ->title ;
305284 }
306285
307- /* Аксессор для получения количества лайков, $post->likes_count*/
308- // public function getLikesCountAttribute()
309- // {
310- // return $this->likes()->where('post_id', $this->id)->count();
311- // }
312-
313- /* Аксессор для определения поставлен ли лайк этим пользователем, $post->is_liked */
314- public function getIsLikedAttribute (): bool
286+ protected function isLiked (): Attribute
315287 {
316- return PostController::isLiked ($ this ->id );
288+ return Attribute:: make (get: fn () => PostController::isLiked ($ this ->id ) );
317289 }
318290}
0 commit comments