尽管多次调用方法,但只能从db查询一次

时间:2019-01-17 10:46:30

标签: php mysql laravel

我正在尝试创建一个Poll(测验)应用程序,现在我正在执行一项功能,该功能可从数据库中获取随机问题并返回民意测验(测试)

我正在按1顺序显示答案,当用户单击“提交答案”时,将运行一个请求并显示下一个答案

问题在于,每次用户提交控制器时,都会再次调用该控制器,并且数据库查询也会被调用(当他单击下一个问题按钮时

这意味着查询现在将获得不同的随机问题,并且由于相同的问题有时会出现两次

我想确保只收到问题的查询被调用一次

型号:

private $random_questions;

public function getRandomQuestions($questions_count)
{
    if (!$this->random_questions)
    {
        $this->random_questions = Question::orderByRaw('RAND()')->take($questions_count)->get();
    }

    return $this->random_questions;
}

public function nextQuestionLink($questions_count, $question_number) {

    $nextQuestionLink = [];

    if ($questions_count != $question_number && $questions_count > $question_number) {
        $nextQuestionLink['url']   = '/polls/random/'.$questions_count.'/'.++$question_number;
        $nextQuestionLink['text']  = 'Следващ въпрос';
        $nextQuestionLink['class'] = 'btn-default';
    } else {
        $nextQuestionLink['url']   = '/result';
        $nextQuestionLink['text']  = 'Приключи';
        $nextQuestionLink['class'] = 'btn-primary';
    }

    return $nextQuestionLink;
}

控制器:

    public function getRandomQuestions($questions_count, $question_number)
    {
        $question = Question::Instance();
        $questions = $question->getRandomQuestions($questions_count);
        $nextQuestionLink = $question->nextQuestionLink($questions_count, 
        $question_number);

        return view('polls.random_questions_poll')->with([
            'question' => $questions[$questions_count-1],
            'next'      => $nextQuestionLink,
        ]);
    }

查看:

@section('content')

<div id="quiz-wrapper">
    <h1>{{ $question->question }}</h1>
    {!! Form::open(array( 'id' => 'message')) !!}
    {!! csrf_field() !!}
    @foreach($question->answers->shuffle() as $answer)
        <h3>
            <div class="form-group">
                <div class="radio">
                    {{Form::radio('result', "$question->id-$answer->id") }}
                    {{ Form::label('result', $answer->name) }}
                </div>
            </div>
        </h3>
    @endforeach
    <a class="next-question-button btn {{ $next['class'] }}" href="{{ $next['url'] }}" style="display: block;" role="button">{{ $next['text'] }}</a>
    <p id="validation-error-container"></p>
    {!! Form::close() !!}
</div>

@endsection

1 个答案:

答案 0 :(得分:0)

由于php默认情况下是无状态的,因此您需要将最初的问题集存储在持久性存储中,并从所有后续请求中将其检索出来。

您可以使用控制器中的会话非常简洁地执行此操作:

public function getRandomQuestions($questions_count, $question_number)
{
    //attempt to retrieve from session
    $questions = session('questions', function(){
        //if not found in session, generate from DB,
        $questions = Question::Instance()->getRandomQuestions($questions_count);
        //and store in session for next request.
        session(['questions' => $questions]);
        return $questions;
    });

    $nextQuestionLink = $question->nextQuestionLink($questions_count, 
    $question_number);

    return view('polls.random_questions_poll')->with([
        'question' => $questions[$questions_count-1],
        'next'      => $nextQuestionLink,
    ]);
}