/**
  * Run the database seeds.
  *
  * @return void
  */
 public function run()
 {
     $json = Storage::get('questions.json');
     $this->command->info($json);
     $questions = json_decode($json, true);
     foreach ($questions['questions'] as $question_keys => $question) {
         $this->command->info("Adding Question: " . $question_keys . "...");
         if ($question["type"] != "true-false") {
             $answers = $question['answers'];
             unset($question['answers']);
         }
         $q = new Question($question);
         $q->save();
         foreach ($question as $question_attribute_name => $question_attribute_value) {
             $this->command->info($question_attribute_name);
         }
         if ($q->type == "true-false") {
             $this->command->info("Adding True/False Question...");
             $answer = $question['answer'];
             $aFalse;
             $aTrue;
             if (Answer::where('text', 'true')->count() >= 1) {
                 $aTrue = Answer::where('text', 'true')->first();
             } else {
                 $aTrue = new Answer(['text' => 'true']);
                 $aTrue->save();
             }
             if (Answer::where('text', 'false')->count() >= 1) {
                 $aFalse = Answer::where('text', 'false')->first();
             } else {
                 $aFalse = new Answer(['text' => 'false']);
                 $aFalse->save();
             }
             $q->answers()->save($aFalse, ['is_correct' => $answer ? 0 : 1]);
             $q->answers()->save($aTrue, ['is_correct' => $answer ? 1 : 0]);
         } else {
             $this->command->info("Adding answers...");
             $answer_ids = array();
             foreach ($answers as $answer_index => $answer) {
                 $this->command->info("Adding " . ($answer_index + 1) . "...");
                 $a = new Answer($answer);
                 $a->save();
                 $q->answers()->save($a, ['is_correct' => $answer['is_correct'] === "false" ? 0 : 1]);
             }
         }
         $this->command->info("");
     }
 }
 public function store(CreateQuestionRequest $request)
 {
     $question = new \App\Question();
     foreach (array_keys($this->fields) as $field) {
         $responce = $request->get($field);
         if ($responce == null && $this->fields[$field] !== '') {
             $responce = $this->fields[$field];
         }
         $question->{$field} = $responce;
     }
     $question->save();
     $formAnswers = $request->get('answers');
     $recivedAnswers = array();
     $validAnswer = $formAnswers[$request->isValid];
     //return $validAnswer;
     for ($index = 0; $index < count($formAnswers); $index++) {
         $recivedAnswers[] = new \App\Answer(array('title' => $formAnswers[$index], 'question_id' => $question->id));
     }
     $question->answers()->saveMany($recivedAnswers);
     $correctAnswerId = \App\Answer::select('id')->where('title', '=', $validAnswer)->where('question_id', '=', $question->id)->get();
     $question->correct_answer_id = $correctAnswerId[0]->id;
     $question->save();
     return redirect::action('Admin\\QuestionController@index')->withSuccess("The question '{$question->title}' was created.");
 }
 public function store_true_false(Request $request)
 {
     $question = new Question();
     $question->prompt = $request->prompt;
     $question->difficulty = $request->difficulty;
     $question->subject_id = 1;
     $question->type = 'true-false';
     $question->total_score = $request->total_score;
     $question->save();
     $answer_true = new Answer();
     $answer_false = new Answer();
     $answer_true->text = $request->choice1;
     $answer_false->text = $request->choice2;
     $answer_true->save();
     $answer_false->save();
     $question->answers()->attach($answer_true->id, array('is_correct' => $request->isCorrect1 != 1 ? 0 : 1));
     $question->answers()->attach($answer_false->id, array('is_correct' => $request->isCorrect2 != 1 ? 0 : 1));
     return redirect('/question');
 }