From 75ebb57dd3ff7f7aac07c288537b55cfefe4d4ee Mon Sep 17 00:00:00 2001 From: Ghostie Date: Wed, 21 Aug 2024 20:36:59 -0500 Subject: [PATCH] displaying comments --- app/Models/Comment.php | 9 ++++++++- app/Models/Post.php | 5 +++++ resources/views/posts/show.blade.php | 17 +++++++++++++++++ 3 files changed, 30 insertions(+), 1 deletion(-) diff --git a/app/Models/Comment.php b/app/Models/Comment.php index a7f9ff9..ab7772a 100644 --- a/app/Models/Comment.php +++ b/app/Models/Comment.php @@ -2,8 +2,10 @@ namespace App\Models; -use Illuminate\Database\Eloquent\Factories\HasFactory; +use App\Models\User; + use Illuminate\Database\Eloquent\Model; +use Illuminate\Database\Eloquent\Factories\HasFactory; class Comment extends Model { @@ -14,4 +16,9 @@ class Comment extends Model "post_id", "comment" ]; + + public function user() + { + return $this->belongsTo(User::class); + } } diff --git a/app/Models/Post.php b/app/Models/Post.php index ddc96f9..f8ed4e7 100644 --- a/app/Models/Post.php +++ b/app/Models/Post.php @@ -24,4 +24,9 @@ class Post extends Model "username" ]); } + + public function comments() + { + return $this->hasMany(Comment::class); + } } diff --git a/resources/views/posts/show.blade.php b/resources/views/posts/show.blade.php index 94c07a0..f91dd7c 100644 --- a/resources/views/posts/show.blade.php +++ b/resources/views/posts/show.blade.php @@ -53,6 +53,23 @@ class="bg-sky-600 hover:bg-sky-700 transition-colors cursor-pointer uppercase font-bold w-full p-3 text-white rounded-lg"> @endauth + +
+ @if ($post->comments->count() > 0) + @foreach ($post->comments as $comment) +
+ + {{ $comment->user->username }} + + +

{{ $comment->comment }}

+

{{ $comment->created_at->diffForHumans() }}

+
+ @endforeach + @else +

No comments yet...

+ @endif +