From e411e6297a8b064db9809a6674ed43e6c6312c34 Mon Sep 17 00:00:00 2001 From: Ghostie Date: Wed, 21 Aug 2024 20:29:52 -0500 Subject: [PATCH] storing the comments --- app/Http/Controllers/CommentController.php | 17 +++++++++++++++-- app/Models/Comment.php | 6 ++++++ resources/views/posts/show.blade.php | 6 ++++++ 3 files changed, 27 insertions(+), 2 deletions(-) diff --git a/app/Http/Controllers/CommentController.php b/app/Http/Controllers/CommentController.php index a0ccc75..3639f7c 100644 --- a/app/Http/Controllers/CommentController.php +++ b/app/Http/Controllers/CommentController.php @@ -2,15 +2,28 @@ namespace App\Http\Controllers; +use App\Models\Post; +use App\Models\User; +use App\Models\Comment; + use Illuminate\Http\Request; class CommentController extends Controller { - public function store() + public function store(User $user, Post $post, Request $request) { // validate + $request->validate([ + "comment" => "required|max:255" + ]); // store - dd("commenting..."); + Comment::create([ + "user_id" => auth()->user()->id, + "post_id" => $post->id, + "comment" => $request->comment + ]); + + return back()->with('message', 'Comment added successfully'); } } diff --git a/app/Models/Comment.php b/app/Models/Comment.php index 0d9ecbd..a7f9ff9 100644 --- a/app/Models/Comment.php +++ b/app/Models/Comment.php @@ -8,4 +8,10 @@ use Illuminate\Database\Eloquent\Model; class Comment extends Model { use HasFactory; + + protected $fillable = [ + "user_id", + "post_id", + "comment" + ]; } diff --git a/resources/views/posts/show.blade.php b/resources/views/posts/show.blade.php index 4058fc5..94c07a0 100644 --- a/resources/views/posts/show.blade.php +++ b/resources/views/posts/show.blade.php @@ -29,6 +29,12 @@ @auth

Add a new Comment

+ @if (session('message')) +

+ {{ session('message') }} +

+ @endif +
@csrf