ideas
ideas copied to clipboard
Make hydrate() also hydrate the Models' Relations
If I have a JSON representation of a Model with a Relation
[{ "id": 1, "title": "One" "relations": [{ "id": 10, "title": "Relation" }] }]
The hydrator can know that relation is a method and can then call that method to get the relationship object. From there is can recursively hydrate relationship classes as well.
+1
+1
Hydrate is useless without this feature. If you want to cache models efficiently without caching full objects, you must convert them to array, but you are unable to hydrate them properly. Im new with Laravel so Im unable to do PR at this point. Maybe later I will feel confident enough, so please fanboys do your job :D
If you call toAarray on a model with relationships, it will include the relationships as well, and hydrate will do the same. The feature that could probably added here is calling hydrate with the nested relations you want to also hydrate but that is no longer hydration but running new sql queries.
Here's a start to an idea for implementation. I'm not sure if a separate method is the best way to go, but probably. More would need to be done to deal with the relation types. I'm not even sure this interacts with them correctly, ex. I don't attempt to deal with pivot models. ideally each with key could pass in a closure to allow for custom code.
https://github.com/laravel/framework/pull/37288
I anyone has any opinions or ideas?