Skip to content

Commit 7814f6c

Browse files
committed
return comment created.
1 parent 1d963ff commit 7814f6c

File tree

2 files changed

+10
-9
lines changed

2 files changed

+10
-9
lines changed

app/Http/Controllers/CommentsController.php

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
use App\Comments;
66
use App\Post;
77
use Illuminate\Http\Request;
8+
use App\Http\Resources\Comments as CommentResource;
89

910
class CommentsController extends Controller
1011
{
@@ -26,8 +27,8 @@ public function index(Post $post)
2627
*/
2728
public function store(Post $post,Request $request)
2829
{
29-
$post->comments()->create($request->all());
30-
return response('created','200');
30+
$comment = $post->comments()->create($request->all());
31+
return response()->json(new CommentResource($comment), 200);
3132
}
3233

3334
/**

app/Http/Resources/Comments.php

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -17,12 +17,12 @@ class Comments extends JsonResource
1717
*/
1818
public function toArray($request)
1919
{
20-
return [
21-
'id' => $this->id,
22-
'body' => $this->body,
23-
'user' => new UserResource($this->user),
24-
'created_at' => $this->created_at->diffForHumans(),
25-
'updated_at' => $this->updated_at->diffForHumans(),
26-
];
20+
return [
21+
'id' => $this->id,
22+
'body' => $this->body,
23+
'user' => new UserResource($this->user),
24+
'created_at' => $this->created_at->diffForHumans(),
25+
'updated_at' => $this->updated_at->diffForHumans(),
26+
];
2727
}
2828
}

0 commit comments

Comments
 (0)