From a1ed206cea7cebe620dcf2ac3cbee82753d818d5 Mon Sep 17 00:00:00 2001 From: PraveenRaamC Date: Mon, 28 Oct 2024 06:16:24 +0530 Subject: [PATCH] #27, Code for adding the answer is done --- .../app/Http/Controllers/ForumController.php | 19 +++++++++++++++++++ .../views/forum/viewAnswer.blade.php | 4 ++-- Alumni-Project/routes/web.php | 1 + 3 files changed, 22 insertions(+), 2 deletions(-) diff --git a/Alumni-Project/app/Http/Controllers/ForumController.php b/Alumni-Project/app/Http/Controllers/ForumController.php index e50ba12..8517054 100644 --- a/Alumni-Project/app/Http/Controllers/ForumController.php +++ b/Alumni-Project/app/Http/Controllers/ForumController.php @@ -2,6 +2,7 @@ namespace App\Http\Controllers; +use App\Models\Answer; use App\Models\Question; use Illuminate\Support\Facades\Session; use Illuminate\Http\Request; @@ -47,5 +48,23 @@ public function storeQuestion(Request $request) return redirect()->route('forum.index')->with('success', 'Question added successfully.'); } + public function storeAnswer(Request $req,$questionId){ + + $id = Session::get('user_id'); + + $req->validate([ + 'body' => 'required|string', + ]); + + $answer = new Answer(); + $answer->answer = $req->body; + $answer->question_id = $questionId; + $answer->alumni_id = $id; + + $answer->save(); + + return redirect()->route('question.answers', ['id' => $questionId]) + ->with('success', 'Your answer has been submitted.'); + } } diff --git a/Alumni-Project/resources/views/forum/viewAnswer.blade.php b/Alumni-Project/resources/views/forum/viewAnswer.blade.php index cc29152..a448209 100644 --- a/Alumni-Project/resources/views/forum/viewAnswer.blade.php +++ b/Alumni-Project/resources/views/forum/viewAnswer.blade.php @@ -24,7 +24,7 @@

{{ $question->body }}


@if(Auth::guard('alumni')->check()) -
+ @csrf
@@ -34,11 +34,11 @@
@endif +
  • -
    diff --git a/Alumni-Project/routes/web.php b/Alumni-Project/routes/web.php index 5fc4f8b..8683f3d 100644 --- a/Alumni-Project/routes/web.php +++ b/Alumni-Project/routes/web.php @@ -169,6 +169,7 @@ // View Forum Route::get('/alumni/forum',[ForumController::class,'viewQuestions']); Route::get('/alumni/forum/answer/{id}',[ForumController::class,'viewAnswers'])->name('question.answers'); + Route::post('/alumni/forum/{id}/answer', [ForumController::class, 'storeAnswer'])->name('answers.store'); // Change password Route::get('/alumni/change-password',[AlumniLoginController::class,'showChangePasswordForm'])->name('alumni.change-password.form');