/** * Store a newly created resource in storage. * * @param Requests\VoteRequest|Request $request * @param $id * @return Response */ public function store(Requests\VoteRequest $request) { // AJAX JSON RESPONSE $response = array('status' => 'success', 'msg' => 'Article has been posted. Redirecting now.'); if (Auth::check()) { \Log::info(Auth::user()); Auth::user()->votes()->create($request->all()); } else { return \Response::json('Nope'); } return \Response::json($response); }
/** * Store a newly created resource in storage. * * NOTE: You are using a FormRequest without any sort of validation.. revise? * * @param Requests\VoteRequest|Request $request * @param $id * @return Response */ public function store(Requests\VoteRequest $request) { $postId = $request->input('postId'); $userId = $request->user()->id; $value = $request->input('value'); // Check to see if there is an existing vote $vote = Vote::wherePostId($postId)->whereUserId($userId)->first(); if (!$vote) { // First time the user is voting Vote::create(['post_id' => $postId, 'user_id' => $userId, 'value' => $value]); } else { $vote->value == $value ? $vote->delete() : $vote->update(['value' => $value]); } // AJAX JSON RESPONSE return response()->json(['status' => 'success', 'msg' => 'Vote has been added.']); }