我有一个具有Vue前端和Laravel后端的测验应用程序。
假设我有一个名为问题的表,其中包含1000多个问题(行)。每个问题都属于许多(或只有一个)模块(另一张表)。
用户选择要查询的模块。测验中默认的问题数为30。这些问题以随机顺序从数据库中提出-一次一次,直到用户最终在数据库中看到该模块的所有问题后才会重复。
做到这一点的最佳方法是什么?
目前这是我正在做的事情
Vue(前端)监视问题计数(默认为30)-达到该计数时-测验结束并显示分数。
用户选择要提问的{module}。
Vue向/ random / all / {module}发送axios GET请求。
此路由调用ModuleController @ random方法,该方法将为该模块返回一个随机问题(用户之前从未见过)。
public function random(Module $module, $name){
// Get all questions for module.
$questions = $module->questions->shuffle()->pluck('id');
// Get unseenQuestions
$sessionName = 'unseenQuestions_' . $name . strval($module->id); //unseenQuestions_all_2
if (session()->has($sessionName)) {
$unseenQuestions = session()->get($sessionName);
} else {
$unseenQuestions = collect($questions);
}
// Pop new Question
$newQuestionID = $unseenQuestions->shift();;
// Store unseenQuestions OR remove unseenQuestions (if it has no items)
if ($unseenQuestions->count() == 0) {
session()->forget($sessionName);
} else {
session()->put($sessionName, $unseenQuestions);
}
return new QuestionResource(Question::find($newQuestionID));
}
您可以说,这利用会话来记住用户有什么问题和尚未看到的问题。我想知道是否有更好的方法可以解决此问题。理想情况下,我希望有一些API端点可以执行此操作,但是我知道API是无状态的,因此使用会话将无效。