@@ -17,20 +17,23 @@ class Post extends JsonResource
1717 */
1818 public function toArray ($ request )
1919 {
20- $ totalCommentPostHas = [];
21- foreach ($ this ->comments as $ key => $ comment ) {
22- $ totalCommentPostHas = new CommentResource ($ comment );
23- }
24- return [
25- 'id ' => $ this ->id ,
26- 'title ' => $ this ->title ,
27- 'slug ' => $ this ->slug ,
28- 'description ' => $ this ->body ,
29- 'category ' => $ this ->category ,
30- 'user ' => new UserResource ($ this ->user ),
31- 'comments ' => $ totalCommentPostHas ,
32- 'created_at ' => $ this ->created_at ->diffForHumans (),
33- 'updated_at ' => $ this ->updated_at ->diffForHumans (),
34- ];
20+ $ totalCommentPostHas = [];
21+ $ totalComments = $ this ->comments ;
22+
23+ foreach ($ totalComments as $ comment ) {
24+ array_push ($ totalCommentPostHas , new CommentResource ($ comment ));
25+ }
26+
27+ return [
28+ 'id ' => $ this ->id ,
29+ 'title ' => $ this ->title ,
30+ 'slug ' => $ this ->slug ,
31+ 'description ' => $ this ->body ,
32+ 'category ' => $ this ->category ,
33+ 'user ' => new UserResource ($ this ->user ),
34+ 'comments ' => $ totalCommentPostHas ,
35+ 'created_at ' => $ this ->created_at ->diffForHumans (),
36+ 'updated_at ' => $ this ->updated_at ->diffForHumans (),
37+ ];
3538 }
3639}
0 commit comments