本文整理汇总了PHP中app\Question::answers方法的典型用法代码示例。如果您正苦于以下问题:PHP Question::answers方法的具体用法?PHP Question::answers怎么用?PHP Question::answers使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类app\Question
的用法示例。
在下文中一共展示了Question::answers方法的3个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的PHP代码示例。
示例1: run
/**
* 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("");
}
}
示例2: store
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.");
}
示例3: store_true_false
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');
}