public function vote($id, $userVote) { // Make sure the capture is real and the vote is valid $vote = new \App\Vote(); $vote->user_id = \Auth::user()->id; $vote->capture_id = $id; $vote->vote = $userVote == 'up' ? 'true' : 'false'; $vote->save(); $capture = \App\Capture::findOrFail($id); return redirect('pokedex/' . $capture->pokemon->name); }
DB::table('movies')->insert($input); }); Route::post('movies/vote', function (Request $request) { $userId = \Auth::user()->id; $input = Input::all(); $vote = App\Vote::where('movie_id', '=', $input['id'])->where('user_id', '=', $userId)->first(); // invert the existing vote if (count($vote)) { if ($vote->vote_up) { $vote->vote_up = 0; } else { $vote->vote_up = 1; } } else { // dd('new'); $vote = new App\Vote(); $vote->user_id = $userId; $vote->movie_id = $input['id']; $vote->vote_up = 1; } if ($vote->save()) { return response()->json('success', 200); } else { return response()->json('error', 500); } }); Route::get('movies', function () { $userId = \Auth::user()->id; return App\Movie::with(['votes' => function ($query) use($userId) { $query->where('user_id', $userId); }])->get();