[英]Laravel Quiz Storing The correct answers
我對 Laravel很陌生,所以我有一個做測驗的項目。 目前,我實現了將帶有無線電答案的問題存儲在數據庫中,但我不知道如何:
1) 在 web 頁面上顯示所有帶有答案的問題。
2) 為用戶存儲每個正確答案的分數。
更新:感謝ettdro ,我已經解決了我的第一個問題。 只剩下我的第二個了。
我將不勝感激任何幫助。 我的Answer.php Model 現在是空的。 我的問題.php Model:
class Question extends Model
{
// connect the models by adding a relationship to the Question model
public function answers()
{
return $this->hasMany(Answer::class);
}
}
我在Migration for Questions中的function 是:
public function up()
{
Schema::create('questions', function (Blueprint $table) {
$table->id();
$table->string('text');
$table->integer('points')->unsigned();
$table->timestamps();
});
}
我在Migration for Answers中的function 是:
public function up()
{
Schema::create('answers', function (Blueprint $table) {
$table->id();
// since answer is connected to the question
$table->integer('question_id');
$table->string('text');
$table->boolean('correct_one');
$table->timestamps();
});
}
我的問題AnswerSeeder.php是:
// for filling the tables
class QuestionAnswerSeeder extends Seeder
{
/**
* Run the database seeds.
*
* @return void
*/
// truncating the tables and then store each question and its answers.
public function run()
{
Question::truncate();
Answer::truncate();
$questionAndAnswers = $this->getData();
$questionAndAnswers->each(function ($question) {
$createdQuestion = Question::create([
'text' => $question['question'],
'points' => $question['points'],
]);
collect($question['answers'])->each(function ($answer) use ($createdQuestion) {
Answer::create([
'question_id' => $createdQuestion->id,
'text' => $answer['text'],
'correct_one' => $answer['correct_one'],
]);
});
});
}
// for the actual data, I use a separate getData method to keep it cleaner
// in this method, I return a big collection with all the questions and answers
private function getData()
{
return collect([
[
'question' => 'When did the World War 2 end?',
'points' => '1',
'answers' => [
['text' => '1939', 'correct_one' => false],
['text' => '1941', 'correct_one' => false],
['text' => '1945', 'correct_one' => true],
],
],
[
'question' => 'Who discovered America?',
'points' => '1',
'answers' => [
['text' => 'Adolf Hitler', 'correct_one' => false],
['text' => 'Napoleon Bonaparte', 'correct_one' => false],
['text' => 'Christopher Columbus', 'correct_one' => true],
],
],
]);
}
}
您應該有一個QuestionController.php
包含以下內容:
/**
* In this function, you need to get all the data you want to pass to your view
* and send it to the compact function in the return statement.
*/
public function index() {
// This will return a collection of questions including their answers.
$questionsCollection = Question::all();
return view('myquestionsview', compact('questionsCollection'));
}
接下來,由於您在索引 function 中返回了myquestionsview
,因此您將需要一個名為myquestionsview.blade.php
的文件位於views
文件夾中。
要顯示你的問題的信息,在你的myquestionsview.blade.php
你應該有類似
@foreach ($questions as $question)
{{ $question->text }}
// Now, we want to display each answers of the question.
@foreach ($question->answers as $answer)
{{ $answer->text }}
@endforeach
@endforeach
這基本上是你想要為你的第一個問題做的事情。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.