Laravel测验存储正确答案

问题描述 投票:0回答:1

我是[[非常新]],到Laravel,所以我有一个测验项目。目前,我已经实现了将带有单选答案的问题存储在数据库中,但是我不知道如何:

1)在网页上显示所有带有答案的问题。

2)为用户存储每个正确答案的分数。

UPDATE:多亏了[[ettdro,我解决了我的第一个问题。仅剩我第二个。我将不胜感激。我的Answer.php

模型目前为空。我的

Question.php模型:class Question extends Model { // connect the models by adding a relationship to the Question model public function answers() { return $this->hasMany(Answer::class); } }

[迁移中的[[问题中的[[我的
向上]函数是:

public function up() { Schema::create('questions', function (Blueprint $table) { $table->id(); $table->string('text'); $table->integer('points')->unsigned(); $table->timestamps(); }); }

<< [迁移
中的[[答案的[我的函数是: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(); }); } 我的
QuestionAnswerSeeder.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], ], ], ]); } }

我对Laravel还是很陌生,所以我有一个测验项目。目前,我已经实现了将带有单选答案的问题存储在数据库中,但是我不知道如何:1)显示所有...
php laravel
1个回答
1
投票
© www.soinside.com 2019 - 2024. All rights reserved.