All related models loaded in memory?

Using the Laravel debugbar I can see some things happening and it’s a little difficult to understand exactly why.

Basically the problem is that all models from a relationship are queried and loaded into memory. I can see them all being loaded as models in the debugbar and the query that fetches them is by far the slowest on the page, while this query is not in my own code. This is the query:

select * from 'comments' where 'comments'.'post_id' in (2)

The problem is that I have thousands of comments so this is slowing things down significantly.

This is the stack trace:
/vendor/laravel/framework/src/Illuminate/Queue/SerializesAndRestoresModelIdentifiers.php:102 /vendor/laravel/framework/src/Illuminate/Queue/SerializesAndRestoresModelIdentifiers.php:57 /vendor/livewire/livewire/src/HydrationMiddleware/HydrateEloquentModelsAsPublicProperties.php:24 /vendor/livewire/livewire/src/RegistersHydrationMiddleware.php:31 /vendor/laravel/framework/src/Illuminate/Support/Facades/Facade.php:261

It looks like that on line 24 in HydrateEloquentModelsAsPublicProperties all the relations are loaded?

new ModelIdentifier($value['class'], $value['id'], $value['relations'], $value['connection'])

This is really outside of what I understand from Livewire so would be appreciated if someone has more in depth knowledge of what’s happening behind the scenes and how I can prevent this from happening if possible.

Hi,

Did you found an answer ? I’m also facing this.

Regards