我必须使用Cache :: remember获得foreach循环内的答案。但是,如果我使用以下解决方案,则所有选项都会重复,因为答案将存储第一个循环的答案并重复。
如何使用每个答案变量中出现的 question_id 来防止这种情况?
@foreach($one_mark_questions as $oneMark_question_number=>$one_mark_question)
@php
$answers = [
['id' => 1, 'option' => $one_mark_question->option1, 'question_id' => $oneMark_question_number],
['id' => 2, 'option' => $one_mark_question->option2, 'question_id' => $oneMark_question_number],
['id' => 3, 'option' => $one_mark_question->option3, 'question_id' => $oneMark_question_number],
['id' => 4, 'option' => $one_mark_question->option4, 'question_id' => $oneMark_question_number]
];
shuffle($answers);
$answers = Cache::remember('answers', 5, function () use ($answers) {
return $answers;
});
@endphp
@endforeach