title: Amending a cast attribute inside a collection in Laravel
body: <p>I have a model has an attribute which is cast to an array, like so</p>
  
  <pre><code>protected $casts = [
     'data' =&gt; 'array',
  ];
  </code></pre>
  
  <p>I need to make an amendment to the array before returning the Collection.  Using the <code>each</code> method on the Collection I can make changes to the attributes inside.</p>
  
  <pre><code>$collection = $collection-&gt;each(function ($collection, $key) {
      if ($collection-&gt;type == 'foo') {
         $collection-&gt;type = 'bar';
      }
  });
  </code></pre>
  
  <p>This works and the Collection is altered.  However I need to change the array in the cast attribute.</p>
  
  <pre><code>$collection = $collection-&gt;each(function ($collection, $key) {
      if ($collection-&gt;type == 'foo') {
  
          foreach ($collection-&gt;data['x'] as $k =&gt; $v) {
              $collection-&gt;data['x'][$k]['string'] = 'example';
          }
  
      }
  });
  </code></pre>
  
  <p>However this returns an error.</p>
  
  <pre><code>Indirect modification of overloaded property App\Models\Block::$data has no effect
  </code></pre>
  
  <p>I understand that accessing $collection->data will be using a magic  <strong>__get()</strong> is being used, so I would need to use a setter.  So how do I achieve this?</p>
  
  <p>Thanks in advance.</p>