public function vote($subName, $slug, $value) { if (!in_array($value, [-1, 0, 1])) { abort(400); } $sub = Sub::where('name', $subName)->firstOrFail(); $post = Post::where('slug', $slug)->where('sub_id', $sub->id)->firstOrFail(); try { $vote = $post->votes()->where('user_id', auth()->id())->firstOrFail(); } catch (ModelNotFoundException $e) { $vote = new Vote(); } $vote->value = $value; $vote->user()->associate(auth()->user()); $vote->voteable()->associate($post); $vote->save(); $post->score += $value; $post->save(); }
public function voteTabDown($id) { $t = Song_data::findOrFail($id); $idUser = \Auth::user()->id; var_dump($id); var_dump($idUser); $checkVote = Vote::where('votable_id', $id)->where('user_id', $idUser)->where('votable_type', 'App\\Song_data')->get(); if ($checkVote->isEmpty()) { $v = new Vote(); $v->vote = -1; $v->user()->associate(\Auth::user()); $v->votable_id = $id; $v->votable_type = "App\\Song_data"; $v->save(); } else { $checkVote = Vote::where('votable_id', $id)->where('user_id', $idUser)->where('votable_type', 'App\\Song_data')->where('vote', 1)->first(); $checkVote->vote = -1; $checkVote->save(); } }