我有一个包含10个测试题的列表,我试图从10个问题中抽出4个给每个学生,我使用foreach shuffle和take(4)。它是有效的,但问题的选项也改变了位置和散布时,我也尝试了随机(4),这也显示了同样的事情。对此的任何修复。下面是代码。
@foreach ($lesson->test->questions->shuffle()->take(4) as $question)
<b>{{ $loop->iteration }}. {{ $question->question }}</b>
<br />
@foreach ($question->options as $option)
<input type="radio" wire:model='question.{{ $question->id }}'
name="questions[{{ $question->id }}]" value="{{ $option->id }}" />
{{ $option->option_text }}<br />
@endforeach
<br />
@endforeach随机过程
@foreach ($lesson->test->questions->random(4) as $question)
<b>{{ $loop->iteration }}. {{ $question->question }}</b>
<br />
@foreach ($question->options as $option)
<input type="radio" wire:model='question.{{ $question->id }}'
name="questions[{{ $question->id }}]" value="{{ $option->id }}" />
{{ $option->option_text }}<br />
@endforeach
<br />
@endforeach发布于 2021-07-12 13:30:20
找到修复后,问题是每当我单击某个选项时,组件都会刷新,这会导致它重新呈现和重新洗牌问题。我所做的是调用wire:ignore,以便在我单击答案时不重新呈现。请参阅下面的代码
<form wire:submit.prevent='lessonTest' action="{{ route('lessons.test', [$lesson->slug]) }}"
method="post">
{{ csrf_field() }}
<div wire:ignore>
@foreach ($lesson->test->questions->random(4) as $questions)
<b>{{ $loop->iteration }}. {{ $questions->question }}</b>
<br />
@foreach ($questions->options as $option)
<input type="radio" wire:model='question.{{ $questions->id }}' name="questions[{{ $questions->id }}]"
value="{{ $option->id }}" required />
{{ $option->option_text }}<br />
@endforeach
<br />
@endforeach
</div>
<button class="btn btn-success btn-lg refresh" type="submit">Submit</button>
</form>https://stackoverflow.com/questions/68327724
复制相似问题