Skip to content

Commit c291e99

Browse files
committed
Fix Comment Feature
1 parent e3e26f1 commit c291e99

File tree

6 files changed

+25
-15
lines changed

6 files changed

+25
-15
lines changed

app/Article.php

+6-1
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,11 @@ public function category()
3131

3232
public function comments()
3333
{
34-
return $this->hasMany(Comment::class)->whereNull('parent_id')->where('status', 'PUBLISH');
34+
return $this->hasMany(Comment::class)->whereNull('parent_id');
35+
}
36+
37+
public function publish_comments()
38+
{
39+
return $this->comments()->where('status', 'PUBLISH');
3540
}
3641
}

app/Comment.php

+6-1
Original file line numberDiff line numberDiff line change
@@ -15,9 +15,14 @@ public function article()
1515

1616
public function child()
1717
{
18-
return $this->hasMany(Comment::class, 'parent_id')->where('status', 'PUBLISH');
18+
return $this->hasMany(Comment::class, 'parent_id');
1919
}
2020

21+
public function publish_child()
22+
{
23+
return $this->child()->where('status', 'PUBLISH');
24+
}
25+
2126
public function getStatusLabelAttribute()
2227
{
2328
//ADAPUN VALUENYA AKAN MENCETAK HTML BERDASARKAN VALUE DARI FIELD STATUS

app/Http/Controllers/Frontend/FrontController.php

+5-5
Original file line numberDiff line numberDiff line change
@@ -13,14 +13,14 @@ class FrontController extends Controller
1313
{
1414
public function index()
1515
{
16-
$articles = Article::with(['category', 'comments'])->orderBy('created_at', 'DESC')->where('status', 'PUBLISH')->paginate(6);
17-
$latest = Article::with(['category', 'comments'])->orderBy('created_at', 'DESC')->where('status', 'PUBLISH')->paginate(3);
16+
$articles = Article::with(['category', 'publish_comments'])->orderBy('created_at', 'DESC')->where('status', 'PUBLISH')->paginate(6);
17+
$latest = Article::with(['category', 'publish_comments'])->orderBy('created_at', 'DESC')->where('status', 'PUBLISH')->paginate(3);
1818
return view('frontend.index', compact('articles', 'latest'));
1919
}
2020

2121
public function article()
2222
{
23-
$articles = Article::with(['category', 'comments'])->orderBy('created_at', 'DESC')->where('status', 'PUBLISH');
23+
$articles = Article::with(['category', 'publish_comments'])->orderBy('created_at', 'DESC')->where('status', 'PUBLISH');
2424

2525
if (request()->q != '') {
2626
$articles = $articles->where('title', 'LIKE', '%' . request()->q . '%');
@@ -34,7 +34,7 @@ public function article()
3434
public function categoryArticle($slug)
3535
{
3636
if (Category::whereSlug($slug)->exists()){
37-
$articles = Category::where('slug', $slug)->first()->article()->with(['category', 'comments'])->orderBy('created_at', 'DESC')->where('status', 'PUBLISH')->paginate(8);
37+
$articles = Category::where('slug', $slug)->first()->article()->with(['category', 'publish_comments'])->orderBy('created_at', 'DESC')->where('status', 'PUBLISH')->paginate(8);
3838
return view('frontend.article', compact('articles'));
3939
}else{
4040
return redirect()->back();
@@ -45,7 +45,7 @@ public function categoryArticle($slug)
4545
public function show($slug)
4646
{
4747
if (Article::whereSlug($slug)->exists()){
48-
$article = Article::with(['category', 'comments', 'comments.child'])->where('slug', $slug)->first();
48+
$article = Article::with(['category', 'publish_comments', 'publish_comments.publish_child'])->where('slug', $slug)->first();
4949
return view('frontend.show', compact('article'));
5050
}else{
5151
return redirect()->back();

resources/views/frontend/article.blade.php

+1-1
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@
2525
<div class="post-meta">
2626
<span class="author mr-2"><img src="{{ asset('front/images/person_rifki.jpg') }}" alt="Colorlib"> {{$row->created_by}}</span>&bullet;
2727
<span class="mr-2">{{$row->category->name}}</span> &bullet;
28-
<span class="ml-2"><span class="fa fa-comments"></span> {{$row->comments->count()}}</span>
28+
<span class="ml-2"><span class="fa fa-comments"></span> {{$row->publish_comments->count()}}</span>
2929
</div>
3030
<h2>{{ $row->title }}</h2>
3131
</div>

resources/views/frontend/index.blade.php

+2-2
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@
2020

2121
<span class="author mr-2"><img src="{{ asset('front/images/person_rifki.jpg') }}" alt="Colorlib"> {{$lat->created_by}}</span>&bullet;
2222
<span class="mr-2">{{$lat->created_at->format('d F Y')}}</span> &bullet;
23-
<span class="ml-2"><span class="fa fa-comments"></span> {{$lat->comments->count()}}</span>
23+
<span class="ml-2"><span class="fa fa-comments"></span> {{$lat->publish_comments->count()}}</span>
2424

2525
</div>
2626
<h3>{{$lat->title}}</h3>
@@ -55,7 +55,7 @@
5555
<div class="post-meta">
5656
<span class="author mr-2"><img src="{{ asset('front/images/person_rifki.jpg') }}" alt="Colorlib"> {{$row->created_by}}</span>&bullet;
5757
<span class="mr-2">{{$row->category->name}}</span> &bullet;
58-
<span class="ml-2"><span class="fa fa-comments"></span> {{$row->comments->count()}}</span>
58+
<span class="ml-2"><span class="fa fa-comments"></span> {{$row->publish_comments->count()}}</span>
5959
</div>
6060
<h2>{{ $row->title }}</h2>
6161
</div>

resources/views/frontend/show.blade.php

+5-5
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@
1515
<div class="post-meta">
1616
<span class="author mr-2"><img src="{{ asset('front/images/person_rifki.jpg')}}" alt="Colorlib" class="mr-2"> {{$article->created_by}}</span>&bullet;
1717
<span class="mr-2">{{$article->created_at->format('d F Y')}}</span> &bullet;
18-
<span class="ml-2"><span class="fa fa-comments"></span> {{$article->comments->count()}}</span>
18+
<span class="ml-2"><span class="fa fa-comments"></span> {{$article->publish_comments->count()}}</span>
1919
</div>
2020
<h1 class="mb-4">{{$article->title}}</h1>
2121
<a class="category mb-5" href="{{ url('/category/' . $article->category->slug) }}">{{$article->category->name}}</a>
@@ -41,9 +41,9 @@
4141
</div>
4242

4343
<div class="pt-5">
44-
<h3 class="mb-5">{{$article->comments->count()}} Comments</h3>
44+
<h3 class="mb-5">{{$article->publish_comments->count()}} Comments</h3>
4545
<ul class="comment-list">
46-
@foreach ($article->comments as $com)
46+
@foreach ($article->publish_comments as $com)
4747
<li class="comment">
4848
<div class="vcard">
4949
<img src="{{ asset('front/images/users.png')}}" alt="Image placeholder">
@@ -53,7 +53,7 @@
5353
<p>{{ $com->comment }}</p>
5454
<p><a href="javascript:void(0)" onclick="balasKomentar({{ $com->id }}, '{{ $com->comment }}')">Reply</a></p>
5555
</div>
56-
@foreach ($com->child as $val)
56+
@foreach ($com->publish_child as $val)
5757
<ul class="children">
5858
<li class="comment">
5959
<div class="vcard">
@@ -126,7 +126,7 @@
126126
<div class="post-meta">
127127
<span class="category">{{$rand->category->name}}</span>
128128
<span class="mr-2">{{$rand->created_at->format('d F Y')}}</span> &bullet;
129-
<span class="ml-2"><span class="fa fa-comments"></span> {{$rand->comments->count()}}</span>
129+
<span class="ml-2"><span class="fa fa-comments"></span> {{$rand->publish_comments->count()}}</span>
130130
</div>
131131
<h3>{{$rand->title}}</h3>
132132
</div>

0 commit comments

Comments
 (0)