title: Laravel / PHP Manipulate Collection Records
body: <p>I'm trying to manipulate an Eloquent object. Below is the structure of the object in Json for ease of reading. Each record is an answer to a question.  You'll notice that there are two instances (1 and 2) in the records. </p>
  
  <p>Here are the objectives:</p>
  
  <p>1) Get all the related "answer" fields into one record (grouped by "instance" -- which defines the relationship -- as discussed below)</p>
  
  <p>2) Group the records by instance. I need all the records with "instance": "1" grouped together and all the records with "instance": "2" grouped together.</p>
  
  <p>3) Maintain the items in a collection to be passed to the view</p>
  
  <p>DESIRED FORMAT: (note how the records are grouped by "instance" and I can now loop through them, which is my objective here)</p>
  
  <pre><code>[
      {
         "1": "John",
         "2": "A",
         "3": "Livingston",
         "instance": "1"
      },
      {
         "1": "Jake",
         "2": "B",
         "3": "Miller"
         "instance": "2"
      }
  ]
  </code></pre>
  
  <p>RAW OUTPUT:</p>
  
  <pre><code>[
  [
      {
          "id": 1241,
          "project_id": 1080,
          "user_id": 1,
          "question_id": 46,
          "answer": "John",
          "instance": "1"
      },
      {
          "id": 1271,
          "project_id": 1080,
          "user_id": 1,
          "question_id": 46,
          "answer": "Jake",
          "instance": "2"
      }
  ],
  [
      {
          "id": 1242,
          "project_id": 1080,
          "user_id": 1,
          "question_id": 47,
          "answer": "A",
          "instance": "1"
      },
      {
          "id": 1272,
          "project_id": 1080,
          "user_id": 1,
          "question_id": 47,
          "answer": "B",
          "instance": "2"
      }
  ],
  
  [
      {
          "id": 1240,
          "project_id": 1080,
          "user_id": 1,
          "question_id": 48,
          "answer": "Livingston",
          "instance": "1"
      },
      {
          "id": 1279,
          "project_id": 1080,
          "user_id": 1,
          "question_id": 48,
          "answer": "Miller",
          "instance": "2"
      }
  ],
  
  ]
  </code></pre>
  
  <p>Here's some code that got me close to the desired outcome but threw an offset error if the number of answers in each instance didn't match exactly:</p>
  
  <pre><code>        // the $answers object is the one I'm trying to manipulate. 
          // for context, $answers-&gt;toJson() gives the raw json output above
              foreach($answers as $answer)
              {
                  $numOfAnswers = $answer-&gt;count();
              }
  
  
              $i = 0;
              while ($i &lt; $numOfAnswers) {
                  $resource{$i} = $answers-&gt;fetch($i);  
                  $i++;
              }   
  
              // This returns 'John' on the first loop, 'Jake' on the second loop, etc.
              foreach($resource as $answerGroup)
              {
                   $answerGroup-&gt;fetch('answer')[1]
              }
  </code></pre>