1

Im developing a small app that is able to create surveys, and in these surveys have question, where a question could me a simple input form or in a form of checkboxes.

My issue is related with the multiple values (radio inputs), i have a table called "answers" with the above structure:

Answers (table):
- id;
- question_id;
- answer;
- email;

In one of my backend pages i have a table where i loop the question and the answers.

Is working fine, but the problem is when i have multiple values, the header table (questions) starts being less the the coluns body answers, and this is because of the number of answers.

I created this query using eloquent and collection where first i orderByGroup of 'email'.

$survey = Survey::find($id);

return $survey->answers->groupBy('email');

It gives me a array of grouped answers from each email, this is great, but now i need to group inside the email groups the questions, in my case 'question_id'. But isnt working for me.

I tried something like

 return $survey->answers->groupBy('email')->groupBy('question_id');

But it doesnt work.

Saumini Navaratnam
  • 8,439
  • 3
  • 42
  • 70
Pedro
  • 1,459
  • 6
  • 22
  • 40

1 Answers1

1

Since the groupBy() method accepts multiple parameters and iterating over them:

public function groupBy(...$groups)
{
    foreach ($groups as $group)

Do this:

return $survey->answers->groupBy('email', 'question_id');
Alexey Mezenin
  • 158,981
  • 26
  • 290
  • 279