david001
2 months ago
102
2
Laravel

How to get Random question but non repeatable

Posted 2 months ago by david001

Hi, i am making a simple quiz. i want to show one question at a time on screen. in my questions table , i have all quiz questions and in answers table 4 options for each question and in results table record of each questions attempt by user. so i should get one random question at a time which is not already in results table

questions table: id,question,created_at,updated_at

answers table:id, question_id,answer,is_correct,created_at,updated_at

results table: id, question_id, users_id, answer ,created_at,updated_at

i'm saving the answer submitted by user in answer column of results table.

Now i want to show only those questions which were not attempt by that particular user. How do i query this??

This is my attempt but unable to combine in one query.

 $question = Question::with('answers')
                    ->inRandomOrder()
                    ->first(); 
                 

        $qid = $question->id;
        $result = Result::where('question_id','!=',$qid)
                       ->where('user_id','!=',1)->first();

Please sign in or create an account to participate in this conversation.