我希望每個帖子都能顯示其喜歡的總數。如何製作一個laravel像櫃檯?
我有這樣一個循環,我blade.php文件:
@foreach ($posts as $post)
<article class="post" data-postid="{{ $post->id }}">
<p>{{ $post->body }}</p>
<div class="info">
Posted by {{ $post->user->first_name }} on {{ $post->created_at }}
</div>
<div class="interaction">
{{ $countlike->where(['post_id' => $post->id])->get()->count() }}<a href="#" class="like"> {{ Auth::user()->likes()->where('post_id', $post->id)->first() ? Auth::user()->likes()->where('post_id', $post->id)->first()->like == 1 ? 'You Liked This Post' : 'Like' : 'Like' }}</a> |
{{ $countdislike->where(['post_id' => $post->id])->get()->count() }}<a href="#" class="like"> {{ Auth::user()->likes()->where('post_id', $post->id)->first() ? Auth::user()->likes()->where('post_id', $post->id)->first()->like == 0 ? 'You Disliked This Post' : 'Dislike' : 'Dislike' }}</a> |
@if(Auth::user() == $post->user)
<a href="#" class="edit">Edit Post</a> |
<a href="{{ route('post.delete', ['post_id' => $post->id]) }}">Delete</a>
@endif
</div>
</article>
<br>
@endforeach
這是我的控制器功能:
public function getDashboard(Request $request) {
$posts = Post::orderBy('created_at', 'desc')->get();
$countlike = Like::where(['like' => '1']);
$countdislike = Like::where(['like' => '0']);
return view('dashboard')->with(['posts' => $posts])->with(['countlike' => $countlike])->with(['countdislike' => $countdislike]);
}
雖然循環中的第一篇文章與ORM進行通信,但其餘部分沒有。 我無法將雄辯的ORM集成到blade.php文件中的循環中,我在這裏做錯了什麼?
你能用不同的詞來解釋你到底想要完成什麼嗎? – Peon