public function postVote(Request $request, $id) { $vote = Vote::find($id); if (!$vote->is_active) { return redirect('/'); } else { $voted = Voter::where('vote_id', '=', $id)->where('ip', '=', sprintf('%u', ip2long($request->ip())))->count(); if ($voted) { return redirect('auth/logout'); } } $inputs = $request->all(); $voter = new Voter(); $voter->ip = $request->ip(); $voter->name = trim($inputs['name']); $voter->department = trim($inputs['department']); $voter->mobile = str_replace(' ', '', $inputs['mobile']); $voter->type_id = $inputs['type']; $voter->vote_id = $id; if ($voter->save()) { Voter::find($voter->id)->nominations()->sync($inputs['vote']); return redirect('vote/statistics/' . $id)->with('status', '投票保存成功'); } else { return back()->withErrors('投票保存失败'); } }
/** * Handle an incoming request. * * @param \Illuminate\Http\Request $request * @param \Closure $next * @return mixed */ public function handle($request, Closure $next) { //If user hasn't selected any options, carry on to form validation / rejection if (!$request->input('option.0')) { return $next($request); } $poll = Option::findOrFail($request->input('option.0'))->poll; //if we already have this user's IP stored and linked to the poll they are trying to vote on //flash an error and redirect to results. if ($poll->ip_checking == 1) { if (Voter::where('ip_address', '=', $request->ip())->where('poll_id', '=', $poll->id)->exists()) { session()->flash('flash_message_confirm', ['title' => 'Error!', 'message' => 'You have already vote on this poll. Your vote has not been counted.', 'type' => 'error']); return redirect('poll/' . $poll->slug . '/result'); } } return $next($request); }
public function getList($id) { $voters = Voter::where('vote_id', '=', $id)->orderBy('created_at')->get(); return view('voter.list', ['title' => '投票者列表', 'voters' => $voters]); }