mirror of
https://github.com/m1ngsama/php.git
synced 2025-12-24 07:56:01 +00:00
Added controllers for comments, voting system, user profiles, and home page. Includes nested comment support and karma calculation.
37 lines
937 B
PHP
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!');
|
|
}
|
|
}
|