Mutation Fails on Eloquent Collections: Understanding Lazy Evaluation
You are attempting to mutate a property on every model within an Eloquent collection, expecting the changes to persist immediately. The code snippet you provided demonstrates a common pattern where developers assume that modifying an object inside a foreach loop will automatically reflect back in the original collection or database state. However, when you inspect $pro->sig() afterward, the new value for $sig->val is missing. This discrepancy stems from how Eloquent handles collections and the distinction between iterating by reference versus by value.
In standard PHP, foreach loops iterate over a copy of the elements, not the original objects themselves. When you run foreach ($pro->sig()->get() as $key => $sig), the collection's internal iterator creates a shallow copy of the models to facilitate iteration. While this might seem harmless at first glance, Eloquent models possess a specific lifecycle tied to their primary keys and relationships that interacts poorly with simple value assignment during iteration.
The core issue lies in the relationship between the parent model ($pro) and the collection returned by $sig(). When you call get() on the relationship, Eloquent returns an instance of Illuminate\Database\Eloquent\Collection. Inside this collection, each item is a model instance. However, simply assigning a value to a property like $sig->val = 2 inside the loop modifies the local variable $sig, not necessarily the underlying state that persists if the relationship is re-evaluated or if the parent object's state is queried again.
More critically, if val is a mass-assignable attribute or if you intend to persist these changes, assigning directly to the property in memory does not trigger database updates. If your expectation is that $pro->sig() will reflect these changes immediately without hitting the database, you are running into Eloquent's eager loading and relationship caching mechanisms. The get() method essentially detaches the models from the active query context for the duration of the loop. Once the loop finishes, the internal state of the collection might be discarded or reset depending on how the parent object manages its relationships.
To truly add value or modify these attributes effectively, you need to ensure you are working with the actual model instances stored in the relationship cache, rather than a transient copy. If val represents data that needs to be persisted, you must use the query builder or model methods to update the records explicitly. For example, instead of looping and assigning, you could iterate over the collection and call $sig->update(['val' => 2]) for each item. This ensures the change is tracked against the primary key and saved to the database.
Alternatively, if you are working with a fresh relationship instance every time, you might need to manipulate the underlying array before re-hydrating it. Consider accessing the raw collection items via $pro->getRelation()->get() and ensuring that the models returned are not being treated as detached copies. If the relationship is defined on the model using hasMany or similar, accessing it without a prior query might trigger a fresh fetch, bypassing any in-memory mutations you tried to apply.
Understanding whether you are modifying in-memory state versus database state is crucial here. If you need the changes to be visible immediately in subsequent calls to $pro->sig(), you must either persist the changes or ensure that the relationship instance being accessed is the same one holding the mutated data. In many cases, simply mutating properties inside a loop on a detached collection will not suffice because Eloquent does not maintain a live link to those specific array keys once the iterator finishes.
For robust applications, always consider using ->each() with closures or explicitly calling save methods when modifying bulk data. This approach guarantees that your mutations are tied to the correct model instances and their respective database rows, preventing silent failures where data appears lost after inspection.