php/app/Http/Controllers/CommentController.php
m1ngsama a158e64985 Implement comment, voting, and user profile features
Added controllers for comments, voting system, user profiles, and home page.
Includes nested comment support and karma calculation.
2025-12-09 16:20:00 +08:00

37 lines
937 B
PHP

<?php
namespace App\Http\Controllers;
use App\Models\Comment;
use App\Models\Post;
use Illuminate\Http\Request;
class CommentController extends Controller
{
public function store(Request $request, Post $post)
{
$validated = $request->validate([
'content' => 'required|string',
'parent_id' => 'nullable|exists:comments,id',
]);
$comment = Comment::create([
'post_id' => $post->id,
'user_id' => auth()->id(),
'parent_id' => $validated['parent_id'] ?? null,
'content' => $validated['content'],
]);
return back()->with('success', 'Comment posted successfully!');
}
public function destroy(Comment $comment)
{
if ($comment->user_id !== auth()->id()) {
abort(403);
}
$comment->delete();
return back()->with('success', 'Comment deleted successfully!');
}
}