55use Illuminate \Http \Resources \Json \JsonResource ;
66use App \Http \Resources \User as UserResource ;
77use App \Http \Resources \Comments as CommentResource ;
8+ use App \Http \Resources \Like as LikeResource ;
89
910
1011class Post extends JsonResource
@@ -17,13 +18,20 @@ class Post extends JsonResource
1718 */
1819 public function toArray ($ request )
1920 {
21+ // Fetch all comments for specific posts.
2022 $ totalCommentPostHas = [];
2123 $ totalComments = $ this ->comments ;
22-
2324 foreach ($ totalComments as $ comment ) {
2425 array_push ($ totalCommentPostHas , new CommentResource ($ comment ));
2526 }
2627
28+ // Fetch all like for specific posts.
29+ $ totalLikePostHas = [];
30+ $ totalLikes = $ this ->likes ;
31+ foreach ($ totalLikes as $ like ) {
32+ array_push ($ totalLikePostHas , new LikeResource ($ like ));
33+ }
34+
2735 return [
2836 'id ' => $ this ->id ,
2937 'title ' => $ this ->title ,
@@ -32,6 +40,7 @@ public function toArray($request)
3240 'category ' => $ this ->category ,
3341 'user ' => new UserResource ($ this ->user ),
3442 'comments ' => $ totalCommentPostHas ,
43+ 'likes ' => $ totalLikePostHas ,
3544 'created_at ' => $ this ->created_at ->diffForHumans (),
3645 'updated_at ' => $ this ->updated_at ->diffForHumans (),
3746 ];
0 commit comments