【发布时间】:2017-08-20 11:32:41
【问题描述】:
我正在尝试在 laravel 中创建自己的赞成/反对票系统,但我不断收到此错误。 但是,仅当用户没有对正在显示的帖子之一投票时才会出现此错误,例如,我正在向用户显示所有帖子并使用 getvote($post_id) 函数进行检查。只有当用户没有对其中一个帖子投票时才会出现错误,但是如果他对所有帖子都投票了,那么错误就不会出现。这是我的代码以澄清更多:
public function getVote($post_id)
{
$username = Session::get('username');
if(isset($username)) {
$uservote = PostVotes::where([
'post_id' => $post_id,
'username' => $username,
])
->first();
$vote = $uservote->vote;
if(!$vote)
{
return;
}
return $vote;
} else {
return null;
}
}
查看
@foreach ($posts as $post)
<div class="container">
<div class="posts">
<div class="card">
{{$post->getVote($post_id)}}
<img class="card-img-top" href="#" src="{{$post->image}}" alt="Card image cap">
<div class="card-block">
<h4 class="card-title">{{$post->title}}</h4>
<div class="vote">
<i class="fa fa-smile-o fa-fw green" aria-hidden="true" data-value="1" data-id="{{$post->post_id}}"></i>
<i class="fa fa-frown-o " aria-hidden="true" data-value="-1" data-id="{{$post->post_id}}"> {{$post->score}}</i>
</div>
<p class="card-text"><small class="text-muted" id="getDate" data-date="{{$post->created_at}}"><i class="fa fa-clock-o fa-fw" aria-hidden="true"></i></small>
by <a href="profile/{{$post->username}}"> {{$post->username}}</a></p>
</div>
</div>
</div>
</div>
@endforeach
【问题讨论】: