1

Any way to reduce memory usage for database objects in Laravel? As i know, we can use toBase() to reduce memory, but when i have relation like this:

$builder = self::select(['id','title'])->where('parent_id', '0');
$builder->with('children');

return $builder->toBase()->get();

toBase converts data into stdclass without children field (but i need that field). So, i need way to use something like toBase for memory reducing but with children preservation. Children have relationship by OneToMany. How can I do that?

John
  • 63
  • 6
  • In the background Laravel first loads all models and then grabs the ids of those models to find all related models for those specific ids and then associates the related models with each base model. If you want to not use eloquent you have to do this yourself manually – apokryfos Oct 23 '21 at 16:12
  • @apokryfos i want use eloquent, but i don`t need properties like connection, table, etc in the resultset – John Oct 23 '21 at 16:16
  • if you can spare the memory to load the results then after you've done that you can use something like `toArray` to get them into an array but you still would need enough memory to load them once – apokryfos Oct 23 '21 at 16:23
  • @apokryfos OK, thank you very much! As i understand, we cannot do memory optimization like described, but we can free some part of memory after allocation using something like toBase(), my understanding is correct? – John Oct 23 '21 at 16:36

0 Answers0