Skip to content

Commit 1d963ff

Browse files
committed
send all comments for specific post with user etc
1 parent 38dc994 commit 1d963ff

File tree

1 file changed

+18
-15
lines changed

1 file changed

+18
-15
lines changed

app/Http/Resources/Post.php

Lines changed: 18 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -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

Comments
 (0)