1

I have 3 tables. Quizzes, Questions and Answers. Columns I want to use are;

Quizzes > id

Questions > quizid (blongs quiz)

Answers > userid, questionid (belongs question)

So my Answers table has no QuizID column. What I want is; group answers table by user ID depending on Question ID that belongs to specified Quiz ID.

In short; I want users that solved the Quiz. But I'm in a dead end :/

My query is like that;

Answer::join('questions', 'questions.id', '=', 'answers.id')
->join('quizzes', 'questions.quiz_id', 'quizzes.id')
->join('users', 'users.id', 'answers.userid')
->where('questions.quiz_id', $id)
->select('users.*')
->groupBy('questions.quiz_id')
->get() 

My mistake is at the groupBy line. I don't want to group questions. I want to group answers that belongs to Quiz's questions. But I don't know how to do that.

7
  • Welcome to Stack Overflow! Please take the tour to learn how Stack Overflow works. Read How to Ask and Minimal, Reproducible Example for know how to improve the quality of your question. Commented Jan 20, 2021 at 18:13
  • seems like you need to learn a couple about query and JOINS. Laravel Joins | QUERIES JOINS Commented Jan 20, 2021 at 18:17
  • @AndréWalker I tried that but I couldn't achieve. Like that: Answer::join('questions', 'questions.id', '=', 'answers.id') ->join('quizzes', 'questions.quiz_id', 'quizzes.id') ->join('users', 'users.id', 'answers.userid')->where('questions.quiz_id', $id)->select('users.*')->groupBy('questions.quiz_id')->get() Commented Jan 20, 2021 at 18:25
  • what was the errors you received? Commented Jan 20, 2021 at 18:26
  • update your question with the attempt and the errors Commented Jan 20, 2021 at 18:27

1 Answer 1

1

You can get list of users that attempted specific quiz as

$users= User::whereHas('answers.question.quiz', function (Builder $query) use ($id) {
                 $query->where('id', $id);
             })->get();

Or from joined query as

$users = DB::table('users as u')
            ->join('answers as a', 'u.id', '=', 'a.userid')
            ->join('questions q', 'q.id', '=', 'a.questionid')
            ->join('quizzes qz', 'q.quiz_id', 'qz.id')
            ->where('qz.id', $id)
            ->select(['u.id','u.name','q.id as qz_id','qz.name as qz_name'])
            ->groupBy(['u.id','u.name','q.id','qz.name'])
            ->get();
Sign up to request clarification or add additional context in comments.

2 Comments

That works perfect! Thank you so much! But I have another question about first code. You used 'answers.question (every column in questions table I guess).quiz (same as before)'. But what if I have different table name than model? (Like in different language). How can I use that line in case of that situation?
@I.Kaya In first query the navigation properties of models are used these are not table names, like in user model there will be a relationship method which points to related answers for that user, in answer model there will be a relationship method which points to question model and same in question model there will be a relationship method that points to quiz model, check docs Querying Relationship Existence

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.