public function vote(VoteRequest $request, $vote, $type, $id) { $user = Auth::user(); if ($type == 'tema') { $type = 'Topic'; $_votable = Topic::findOrFail($id); } else { $type = 'Reply'; $_votable = Reply::findOrFail($id); } $_vote = Vote::where('votable_type', $type)->where('votable_id', $id)->where('user_id', $user->id)->first(); //Ištrinam visus balsus, jei netyčia susibugintų. if ($_vote) { Vote::where('votable_type', $type)->where('votable_id', $id)->where('user_id', $user->id)->delete(); if ($_vote->is != $vote) { $created_vote = Vote::create(['user_id' => $user->id, 'votable_type' => $type, 'votable_id' => $id, 'is' => $vote]); //cancel old vote and give new vote if ($vote == 'upvote') { event(new UpVoted($_votable, $created_vote, $_votable->user, true)); } else { event(new DownVoted($_votable, $created_vote, $_votable->user, true)); } } else { //cancel current vote if ($vote == 'upvote') { event(new DownVoted($_votable, $_vote, $_votable->user, false)); } else { event(new UpVoted($_votable, $_vote, $_votable->user, false)); } } } else { $created_vote = Vote::create(['user_id' => $user->id, 'votable_type' => $type, 'votable_id' => $id, 'is' => $vote]); //just vote if ($vote == 'upvote') { event(new UpVoted($_votable, $created_vote, $_votable->user, false)); } else { event(new DownVoted($_votable, $created_vote, $_votable->user, false)); } } return response('success', 200); }
public function vote($id, $votable, $type) { //patikrinti ar gali balsuoti if ($this->can_vote) { $vote = $this->hasVoted($id, $votable); //patikrinti ar zmogus jau balsavo if ($vote) { //pasalina pries tai buvusi balsa $vote->delete(); } //balsuoja is naujo Vote::create(['votable_type' => $votable, 'votable_id' => $id, 'user_id' => $this->id, 'is' => $type]); } }