Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

asdasdsa's avatar

Livewire, show questions one-by-one?

I have just started checking out livewire, and need some help wrapping my head around this. Lest say that i have a model Question and a model Choice. A question hasMany choices, and a choice belongsTo a question. I create a livewire view php artisan make:livewire Quiz and i pass $questions => Question::with('choices')->get() to that view. I then create another view, to display the actuall questions, one-by-one php artisan make:livewire showQuestions, but how can I go through the questions one by one by clicing a button? Should be fairly simple but i have no ide :D

0 likes
3 replies
bugsysha's avatar

How do you store answers? If the answers table has user_id and question_id you can do following.

$user = $request->user();
$answeredQuestions = $user->answers()->pluck('question_id');
$questions = Question::whereNotIn('id', $answeredQuestions)->get();

That way you will always get the next question that is without an answer for the current user.

asdasdsa's avatar

@bugsysha Thanks! Currently i dont store the answers, but I will probably do that once i get the basics up and running :)

What i want to do is to grab a collection $questions => Question::with('choices')->get() and then from a livewire view, go through the collection one by one bu clicking a button (and then evaluate if the user clicked the correct alternative).

bugsysha's avatar

Store answered question IDs on Livewire component until you start storing it in the database and use same algorythm to exclude answered questions.

Or you can store answered question IDs on Livewire component and also store collection of questions on Livewire component and key them by ID. Then you can get the following question from that collection based on the next ID.

I would advise not to postpone adding answers. Add them right away and use them as described in my first reply. That can save a lot of time that you might spend if you overlook something.

Please or to participate in this conversation.