function editGameQuestionsData() { $rules = array(); foreach (Input::get('question') as $key => $val) { $rules['question.' . $key . '.question.question_text'] = 'required'; foreach (Input::get('question')[$key]['question']['answers'] as $key2 => $val) { $rules['question.' . $key . '.question.answers.' . $key2] = 'required'; } } $messages = array('required' => 'שדה :attribute הינו שדה חובה'); // do the validation ---------------------------------- // validate against the inputs from our form $validator = Validator::make(Input::all(), $rules, $messages); // check if the validator failed ----------------------- if ($validator->fails()) { // get the error messages from the validator $messages = $validator->messages(); // redirect our user back to the form with the errors from the validator return redirect()->back()->withErrors($validator)->withInput(Input::all()); } else { $qs = Questions::where('game_id', Input::get('game_id'))->get(); foreach ($qs as $q) { Answers::where('question_id', $q->id)->delete(); $q->delete(); } foreach (Input::get('question') as $qus) { $questions = new Questions(); $questions->game_id = Input::get('game_id'); $questions->question = $qus['question']['question_text']; $questions->save(); $index = 0; foreach ($qus['question']['answers'] as $ans) { $answers = new Answers(); $answers->right_answer = $index++ == (int) $qus['question']["right_answer"] ? 1 : 0; $answers->answer = $ans; $answers->question_id = $questions->id; $answers->save(); } } } return redirect('/game/' . Games::find(Input::get('game_id'))->unique_id); }