Bladeren bron

changes in progress

tyson 1 jaar geleden
bovenliggende
commit
574aa3e4ee

+ 20 - 0
app/Http/Controllers/CommentController.php

@@ -0,0 +1,20 @@
+<?php
+
+namespace App\Http\Controllers;
+
+use App\Models\Comment;
+use App\Models\Idea;
+use Illuminate\Http\Request;
+
+class CommentController extends Controller
+{
+    public function store(Idea $idea)
+    {
+        $comment = new Comment();
+        $comment->idea_id = $idea->id;
+        $comment->content = \request()->get("content");
+        $comment->save();
+
+        return redirect()->route("ideas.show", $idea->id)->with("success", "Comment created successfully");
+    }
+}

+ 11 - 0
app/Models/Comment.php

@@ -0,0 +1,11 @@
+<?php
+
+namespace App\Models;
+
+use Illuminate\Database\Eloquent\Factories\HasFactory;
+use Illuminate\Database\Eloquent\Model;
+
+class Comment extends Model
+{
+    use HasFactory;
+}

+ 5 - 0
app/Models/Idea.php

@@ -13,4 +13,9 @@ class Idea extends Model
         'content',
         'likes'
     ];
+
+    public function comments()
+    {
+        $this->hasMany(Comments::class)
+    }
 }

+ 29 - 0
database/migrations/2024_03_31_150332_create_comments_table.php

@@ -0,0 +1,29 @@
+<?php
+
+use Illuminate\Database\Migrations\Migration;
+use Illuminate\Database\Schema\Blueprint;
+use Illuminate\Support\Facades\Schema;
+
+return new class extends Migration
+{
+    /**
+     * Run the migrations.
+     */
+    public function up(): void
+    {
+        Schema::create('comments', function (Blueprint $table) {
+            $table->id();
+            $table->foreignId("idea_id")->constrained()->cascadeOnDelete();
+            $table->string("content");
+            $table->timestamps();
+        });
+    }
+
+    /**
+     * Reverse the migrations.
+     */
+    public function down(): void
+    {
+        Schema::dropIfExists('comments');
+    }
+};

+ 33 - 0
resources/views/shared/comments-box.blade.php

@@ -0,0 +1,33 @@
+<div>
+    <form action="{{route("ideas.comments.store", $idea->id)}}" method="post">
+        @csrf
+    <div class="mb-3">
+        <textarea id="comment-content" name="content" class="fs-6 form-control" rows="1"></textarea>
+    </div>
+    <div>
+        <button type="submit" class="btn btn-primary btn-sm"> Post Comment </button>
+    </div>
+    </form>
+    <hr>
+    <div class="d-flex align-items-start">
+        <img style="width:35px" class="me-2 avatar-sm rounded-circle"
+             src="https://api.dicebear.com/6.x/fun-emoji/svg?seed=Luigi"
+             alt="Luigi Avatar">
+        <div class="w-100">
+            <div class="d-flex justify-content-between">
+                <h6 class="">Luigi
+                </h6>
+                <small class="fs-6 fw-light text-muted"> 3 hour
+                    ago</small>
+            </div>
+            <p class="fs-6 mt-3 fw-light">
+                and 1.10.33 of "de Finibus Bonorum et Malorum" (The Extremes of Good and
+                Evil)
+                by
+                Cicero, written in 45 BC. This book is a treatise on the theory of ethics,
+                very
+                popular during the Renaissan
+            </p>
+        </div>
+    </div>
+</div>

+ 1 - 31
resources/views/shared/idea-card.blade.php

@@ -53,36 +53,6 @@
                                         {{$idea->created_at->format("n-d-Y")}} </span>
             </div>
         </div>
-        <div>
-            <div class="mb-3">
-                <textarea class="fs-6 form-control" rows="1"></textarea>
-            </div>
-            <div>
-                <button class="btn btn-primary btn-sm"> Post Comment </button>
-            </div>
-
-            <hr>
-            <div class="d-flex align-items-start">
-                <img style="width:35px" class="me-2 avatar-sm rounded-circle"
-                     src="https://api.dicebear.com/6.x/fun-emoji/svg?seed=Luigi"
-                     alt="Luigi Avatar">
-                <div class="w-100">
-                    <div class="d-flex justify-content-between">
-                        <h6 class="">Luigi
-                        </h6>
-                        <small class="fs-6 fw-light text-muted"> 3 hour
-                            ago</small>
-                    </div>
-                    <p class="fs-6 mt-3 fw-light">
-                        and 1.10.33 of "de Finibus Bonorum et Malorum" (The Extremes of Good and
-                        Evil)
-                        by
-                        Cicero, written in 45 BC. This book is a treatise on the theory of ethics,
-                        very
-                        popular during the Renaissan
-                    </p>
-                </div>
-            </div>
-        </div>
+        @include('shared.comments-box')
     </div>
 </div>

+ 2 - 0
routes/web.php

@@ -28,6 +28,8 @@ Route::put('/ideas/{idea}', [\App\Http\Controllers\IdeaController::class, 'updat
 
 Route::delete('/ideas/{idea}', [\App\Http\Controllers\IdeaController::class, 'destroy'])->name('ideas.destroy');
 
+Route::post('/ideas/{idea}/comments', [\App\Http\Controllers\CommentController::class, 'store'])->name('ideas.comments.store');
+
 Route::get('/terms', function (){
     return view('terms');
 });