CommentController.php
931 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
<?php
namespace App\Http\Controllers;
use App\Http\Requests\Comment\DestroyRequest;
use App\Http\Requests\Comment\StoreRequest;
use App\Http\Resources\CommentCollection;
use App\Http\Resources\CommentResource;
use App\Models\Article;
use App\Models\Comment;
class CommentController extends Controller
{
protected Comment $comment;
public function __construct(Comment $comment)
{
$this->comment = $comment;
}
public function index(Article $article)
{
return new CommentCollection($article->comments);
}
public function store(Article $article, StoreRequest $request)
{
$comment = $article->comments()->create(['body' => $request->comment['body'], 'user_id' => auth()->id()]);
return new CommentResource($comment);
}
public function destroy(Article $article, Comment $comment, DestroyRequest $request): void
{
$comment->delete();
}
}