CoolFace
Apppublic

kenken999/php

sourceHugging Faceupdated 2y agoView on Hugging Face
0likes
CommentController.php38 linesDownload Raw Back to Controllers
1<?php2 3namespace App\Http\Controllers;4 5use App\Http\Requests\Comment\DestroyRequest;6use App\Http\Requests\Comment\StoreRequest;7use App\Http\Resources\CommentCollection;8use App\Http\Resources\CommentResource;9use App\Models\Article;10use App\Models\Comment;11 12class CommentController extends Controller13{14    protected Comment $comment;15 16    public function __construct(Comment $comment)17    {18        $this->comment = $comment;19    }20 21    public function index(Article $article)22    {23        return new CommentCollection($article->comments);24    }25 26    public function store(Article $article, StoreRequest $request)27    {28        $comment = $article->comments()->create(['body' => $request->comment['body'], 'user_id' => auth()->id()]);29 30        return new CommentResource($comment);31    }32 33    public function destroy(Article $article, Comment $comment, DestroyRequest $request): void34    {35        $comment->delete();36    }37}38