Did you try the
inRandomOrder
Reference:
Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.
Hello everyone, im creating a quiz app for driving lessons. the quiz page have all the data but i want to do next button to take you to the next question but in in a random order.
this is my controller's function for the next but it's not working it worked with me in another project
public function submitans(Request $request) {
$nextq = Session::get('nextq');
$wrongans = Session::get('wrongans');
$correctans = Session::get('correctans');
$validate = $request->validate([
'ans' => "required",
'dbans' => 'required'
]);
$nextq = Session::get('nextq');
$nextq += 1;
if ($request->dbans == $request->ans) {
$correctans += 1;
} else {
$wrongans += 1;
}
Session::put("nextq", $nextq);
Session::put("wrongans", $wrongans);
Session::put("correctans", $correctans);
$i = 0;
$questions = question::all();
foreach ($questions as $question) {
$i++;
if ($questions->count() < $nextq) {
return view('pages.end');
}
if ($i == $nextq) {
// $question = Question::where('id', '>', $question->id)->orderBy('id')->first();
return view('pages.questions.quiz')->with(['question' => $question]);
}
}
}"
and this is a part of the view "
@csrf......
<h5 class="mt-1 ml-2">{{ $question->title }}</h5>
</div>
<div class="text-center">
<img src="{{ asset('storage/' . $question->image) }}" alt="image" class="rounded">
</div>
<div class="form-check">
<input class="form-check-input" type="checkbox" id="flexCheckDefault"
name="ans" checked="true" />
<label class="form-check-label" for="flexCheckDefault">{{ $question->reponse1 }}</label>
</div>
and the routing
Route::get('/quiz',[QuestionController::class,'index']); Route::any('/submitans', [QuestionController::class, 'submitans']);
Please or to participate in this conversation.