Laravel : 防止用户投票两次
Laravel : Prevent the user from voting twice
我想阻止用户投票两次并将他们重定向到结果页面。
控制器
public function view_survey(Survey $survey)
{
$survey->option_name = unserialize($survey->option_name);
$answers = \App\Answer::pluck('Answer', 'id')->toArray();
return view('survey.view', compact('survey', 'answers'));
}
表格:
Table of Answers
执行此操作的基本方法是:
public function view_survey(Survey $survey)
{
// get you logged in user
$user_id = auth()->user()->id;
$survey->option_name = unserialize($survey->option_name);
$hasAnswer = \App\Answer::where(['user_id'=>$user_id , 'survey_id' => $survey->id])->exists();//check if user has answer
//user has an answer to one of the survey questions. Alternatively, you can also check if all questions are answered, or if a specific question is answered by including question id as well in where
if($hasAnswer){
return redirect('routetoshowsurvey',$survey_id);
}
return view('survey.view', compact('survey', 'answers'));
}
我想阻止用户投票两次并将他们重定向到结果页面。
控制器
public function view_survey(Survey $survey)
{
$survey->option_name = unserialize($survey->option_name);
$answers = \App\Answer::pluck('Answer', 'id')->toArray();
return view('survey.view', compact('survey', 'answers'));
}
表格: Table of Answers
执行此操作的基本方法是:
public function view_survey(Survey $survey)
{
// get you logged in user
$user_id = auth()->user()->id;
$survey->option_name = unserialize($survey->option_name);
$hasAnswer = \App\Answer::where(['user_id'=>$user_id , 'survey_id' => $survey->id])->exists();//check if user has answer
//user has an answer to one of the survey questions. Alternatively, you can also check if all questions are answered, or if a specific question is answered by including question id as well in where
if($hasAnswer){
return redirect('routetoshowsurvey',$survey_id);
}
return view('survey.view', compact('survey', 'answers'));
}