1日1回答えられるクイズサイトを作っています。
前回の質問でSessionを使った方が良いというアドバイスをいただき、実装しようと試行錯誤しましたが、Seesionを用いて解答済かどうかの処理をどのようにすれば良いのかわかりません。
OS:Mac M1
Laravel Framework 6.20.7
Quiz.vueのdata()内でisQuizFinishやisAlreadyAnsweredなどのクイズに解答したかどうかの変数が定義されていているのでこれを使いたいのですが、どうにもSessionと組み合わせて使う方法がわかりません。どうかよろしくお願いします。
Quiz.vue
<template> <div class="container is-fullhd"> <main> <div class="container"> <div class="notification"> <div class="columns"> <div class="card"> <div class="card-content"> <div class="column"> <section> <h2 cclass="title is-4"> 問題 </h2> <p>{{ title }}</p> <div class="quiz-answer__list"> <ul> <li v-for="(answer, index) in answers" :key="index"> <a> <button @click="goAnswer(index + 1)" :disabled="isAlreadyAnswered" class="button is-light is-fullwidth" >{{ index + 1 }}</button> </a> {{ answer }} </li> </ul> </div> </section> <section> <h2 class="title is-4"> 正解 </h2> <p> <button class="quiz-correct-answer button is-light is-fullwidth" v-show="isAlreadyAnswered" disabled >{{ correctAnswerNo }}</button> </p> <button @click="goAnswer(0)" v-show="!isAlreadyAnswered">正解を表示する</button> <div class="alert alert-info" v-show="isCorrect"> <strong>正解です</strong> </div> <div class="alert alert-danger" v-show="isMistake"> <strong>間違っています!</strong> </div> <div class="quiz-commentary__text" v-show="isAlreadyAnswered" style="white-space:pre-wrap; word-wrap:break-word;" >{{ commentary }}</div> <button type="button" data-toggle="modal" data-target="#modal-result" class="center-block" v-show="isQuizFinish" @click="showResult" >結果を見る</button> <button class="button is-info is-centered">クイズ一覧</button> </section> </div> </div> </div> <div class="submenu column is-4"> <the-sidebar></the-sidebar> </aside> </div> </div> </div> </div> </main> <the-modal :correctPercentageObject="correctPercentageObject" ref="modal" ></the-modal> </div> </template> <script> import TheSidebar from "../layout/TheSidebar"; import TheModal from "../module/TheModal"; export default { components: { TheSidebar, TheModal, }, mounted() { this.getQuiz(); }, data() { return { quizData: [], title: "", imageSrc: "", answers: [], commentary: "", correctAnswerNo: 0, isCorrect: false, //正解かどうか isMistake: false, //間違いかどうか isAlreadyAnswered: false, //回答済みかどうか isQuizFinish: false, //クイズが終了したかどうか score: 0, quizNumber: 1, correctPercentageObject: {}, }; }, methods: { getQuiz() { this.$http.get('/api/quiz') .then(res => { this.quizData = res.data const quiz = this.quizData[0] this.title = quiz.title this.answers = [ this.quizData[0].answer.answer_1, this.quizData[0].answer.answer_2, this.quizData[0].answer.answer_3, this.quizData[0].answer.answer_4 ]; this.commentary = this.quizData[0].answer.commentary; this.correctAnswerNo = this.quizData[0].answer.correct_answer_no; console.log(this.quizData); }) }, goAnswer(selectAnswerNum) { if (selectAnswerNum === 0) { this.isCorrect = false; this.isMistake = false; } else if (selectAnswerNum === Number(this.correctAnswerNo)) { // 正解を押した場合 alert-infoを表示し、alert-dangerを非表示にする そしてスコアを加算する this.isCorrect = true; this.isMistake = false; this.score += 1; } else { // 不正解の場合 alert-infoを非表示し、alert-dangerを表示にする this.isMistake = true; this.isCorrect = false; } this.isAlreadyAnswered = true; if (this.quizNumber >= 1) { this.endQuiz(); console.log(this.endQuiz()); } }, endQuiz() { this.isQuizFinish = true; this.answerNo = "-"; this.isAlreadyAnswered = true; this.correctPercentageObject = { correctScore: this.score, mistakeScore: 10 - this.score }; }, showResult() { this.$refs.modal.render(); } } }; </script>
web.php
<?php Route::get('/', function() { return view('quiz.index'); }); Route::get('/quiz', function() { return view('quiz.show'); });
api.php
Route::group(['middleware' => ['api']], function () { Route::get('quiz', 'Api\QuizController@show'); Route::post('quiz', 'Api\QuizController@store'); });
Quiz Controller.php
<?php namespace App\Http\Controllers\Api; use App\Http\Controllers\Controller; use Illuminate\Http\Request; class QuizController extends Controller { public function show() { $quiz = cache('quiz'); return $quiz; } public function store(Request $request) { $request->session()->put('Answertime', 19); $isAnswer = $request->session()->get('Answertime'); } }
Usersテーブル
<?php use Illuminate\Database\Migrations\Migration; use Illuminate\Database\Schema\Blueprint; use Illuminate\Support\Facades\Schema; class CreateUsersTable extends Migration { /** * Run the migrations. * * @return void */ public function up() { Schema::create('users', function (Blueprint $table) { $table->bigIncrements('id'); $table->string('name'); $table->string('email')->unique(); $table->timestamp('email_verified_at')->nullable(); $table->string('password'); $table->rememberToken(); $table->timestamps(); }); } /** * Reverse the migrations. * * @return void */ public function down() { Schema::dropIfExists('users'); } }
回答2件
あなたの回答
tips
プレビュー
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。
2021/05/05 06:40
2021/05/05 07:04
2021/05/09 05:47
2021/05/09 06:38