CommentController.php 941 B

12345678910111213141516171819202122232425262728293031
  1. <?php
  2. namespace App\Http\Controllers;
  3. use Illuminate\Http\Request;
  4. use App\Models\Comment;
  5. use App\Models\Post;
  6. use App\Events\CommentCreated;
  7. class CommentController extends Controller
  8. {
  9. public function index(){
  10. return view('comments.moderation', ['unapprovedComments' => Comment::where('is_approved', false)->get()]);
  11. }
  12. public function store(Request $request, Post $post){
  13. $comment = $post->comments()->create($request->validate([
  14. 'author_name' => 'required',
  15. 'content' => 'required'
  16. ]));
  17. event(new CommentCreated($comment));
  18. return back()->with('success', 'Комментарий отправлен на модерацию');
  19. }
  20. public function approve($id) {
  21. $comment = Comment::findOrFail($id);
  22. $comment->update(['is_approved' => true]);
  23. return back()->with('success', 'Комментарий одобрен');
  24. }
  25. }