title: Adding a new property to a Laravel collection
body: <p>I access a couple collections like this,</p>
  
  <pre><code>    $articleActions = EloArticleReferenceAction
          ::where('file', '=', $file)
          -&gt;get()
          -&gt;keyBy('type');
  
      $referencesWithValidDois = EloDoi
          ::where('file', '=', $file)
          -&gt;get();
  </code></pre>
  
  <p>And I want to merge them. I cant use <code>merge</code> because some IDs in both objects are similar, and therefore one will overwrite the other. Instead, I am doing this:</p>
  
  <pre><code>    $response = collect();
  
      foreach ($articleActions as $articleAction) {
          $response-&gt;push($articleAction);
      }
  
      foreach ($referencesWithValidDois as $referencesWithValidDoi) {
          $response-&gt;doi-&gt;push($referencesWithValidDoi);
      }
  </code></pre>
  
  <p>However it break here. And when I do something like this instead:</p>
  
  <pre><code>    $response = collect();
  
      foreach ($articleActions as $articleAction) {
          $response-&gt;push($articleAction);
      }
  
      $response['doi'] = [];
  
      foreach ($referencesWithValidDois as $referencesWithValidDoi) {
          $response['doi'] = $referencesWithValidDoi;
      }
  </code></pre>
  
  <p>It kinda works, but it sends back an object like this:</p>
  
  <p><a href="https://i.stack.imgur.com/U0cEE.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/U0cEE.png" alt="img"></a></p>
  
  <p>In which the <code>doi</code> property gets overwritten with the current <code>$referencesWithValidDoi</code> in the iteration.</p>
  
  <p>So, currently, it is sent back as:</p>
  
  <pre><code>    0: {...},
      1: {...},
      2: {...},
      3: {...},
      doi: {...}
  </code></pre>
  
  <p>But how can I write it, so that it is sent back as:</p>
  
  <pre><code>    0: {...},
      1: {...},
      2: {...},
      3: {...},
      doi: {
          0: {...},
          1: {...},
          2: {...},
          ...
      }
  </code></pre>
  
  <hr>
  
  <p>Edit: Doing it like this,</p>
  
  <pre><code>    $response = collect();
  
      foreach ($articleActions as $articleAction) {
          $response-&gt;push($articleAction);
      }
  
      $response['doi'] = [];
  
      foreach ($referencesWithValidDois as $referencesWithValidDoi) {
          $response['doi'][] = $referencesWithValidDoi;
      }
  </code></pre>
  
  <p>Throws an error:</p>
  
  <p><code>Indirect modification of overloaded element of Illuminate\Support\Collection has no effect</code></p>