我有这个PHP代码它会进行测试并在测试结果为真时运行它。但即使它得到错误的结果,CastResult也会忽略if check。
private function canVote($user,$id)
{
$userVoted = VoteModel::where('user',$user)->where('post',$id)->get();
if(count($userVoted)==0)
{
$canVote=true;
}
else {
$canVote=false;
}
return $canVote;
}
private function castVote($mode)
{
$user = Auth::getUser();
$id = $this->param('id');
if($this->canVote($user,$id))
{
$newVote = new VoteModel;
$newVote->user = $user['name'];
//other stuff
} else
{
Flash::error('Err');
}
}
答案 0 :(得分:0)
更简单明了,没有未定义的变量:
private function canVote($user,$id)
{
$userVoted = VoteModel::where('user',$user)->where('post',$id)->get();
return ( count($userVoted) === 0 );
}