我希望每个帖子都能显示其喜欢的总数。如何制作一个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