ideas icon indicating copy to clipboard operation
ideas copied to clipboard

Make hydrate() also hydrate the Models' Relations

Open dariusj18 opened this issue 7 years ago • 5 comments

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.

dariusj18 avatar Feb 23 '18 22:02 dariusj18

+1

navigatetravel avatar Nov 06 '18 23:11 navigatetravel

+1

tquiroga avatar Jun 20 '19 08:06 tquiroga

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

vytsci avatar Oct 17 '19 09:10 vytsci

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.

xwiz avatar Apr 19 '21 08:04 xwiz

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?

dariusj18 avatar May 06 '21 17:05 dariusj18