public function saveAnswer(Request $request) { if ($request->input('is_test') != 1) { $applicant = Applicant::where('form_key', $request->input('form_key'))->first(); $blueprint = Blueprint::find($applicant->blueprint_id); $question = Question::find($request->input('question_id')); $answer = Answer::firstOrCreate(["blueprint_id" => $blueprint->id, "question_id" => $question->id, "form_key" => $applicant->form_key]); if ($question->type == "integer" || $question->type == "number") { $answer->num_value = $request->input('question_value'); $answer->text_value = null; } else { $answer->num_value = null; $answer->text_value = $request->input('question_value'); } $answer->update(); $answer->question_value = $request->input('question_value'); $answer->new_token = csrf_token(); } else { $question = Question::find($request->input('question_id')); $answer = new Answer(["blueprint_id" => $question->blueprint->id, "question_id" => $question->id, "form_key" => "xxx"]); $answer->num_value = $question->type == "number" ? $request->input('question_value') : null; $answer->text_value = $question->type != "number" ? $request->input('question_value') : null; $answer->question = $question; //$answer->update(); $answer->question_value = $request->input('question_value'); $answer->new_token = csrf_token(); } return response()->json($answer)->header('Access-Control-Allow-Origin', '*'); }