[12.x] Retain associative keys on eager loaded relations#58506
[12.x] Retain associative keys on eager loaded relations#58506taylorotwell merged 3 commits intolaravel:12.xfrom
Conversation
|
I understand why you modified
Instead of modifying One other minor note, since keys can be either |
|
Just to make sure the functionality stayed the same, I rewrote the function by copy-pasting the code from |
|
Thanks 👍 |
|
After updating to Laravel 12.50.0 |
I'm not experiencing the same (just checked to make sure). In principle, this code should only affect relations that already returned associative arrays when lazily loaded. What kind of relation are you having this issue with? |
|
We are using the OctoberCMS AttachMany relation (https://github.com/octobercms/library/blob/4.x/src/Database/Relations/AttachMany.php) which is based on the MorphMany relation. The other relations are still working as before, indeed. |
Cool, I will have a look later today. |
|
@Jamerze Took a bit longer to find some time. But, unfortunately I've been unable to replicate your issue in a simple testing environment. I don't get any difference (comparing Are you able to give me a working example where it goes wrong? If I can reproduce it I'm happy to debug and look for a fix. |
|
@Jade-GG Thanks for testing this. I tested it as well and it looks more like an OctoberCMS issue rather than something caused by this PR. I’ll open an issue there to check whether the functionality broke due to the Laravel update or something else. |
Fixes #57754.
TL:DR; this specifically arose from a very niche use case in one of our projects, where you may try to use the (currently undocumented)
afterQueryfunction as part of an eager loaded relation, in which that afterQuery causes your collection to get become an associative collection. For example:If you use lazy loading, e.g.,
User::first()->posts, this already worked as I would have expected. However, when using eager loading, e.g.,User::with('posts')->first()->poststhe resulting array would become non-associative due to how the dictionary is being built. This PR changes that behaviour specifically for the case that the resulting array out of a relation is associative.This (as far as I can tell) will not have any bad side effects as I've made sure this will only occur on non-associative arrays and only specifically in the dictionaries of relations. Non-associative arrays there won't normally occur unless you specifically want them to be there.
Of course I don't have the most in-depth knowledge about the inner workings of the core framework, so I could be wrong, however I wasn't able to find any counterexamples.