简体   繁体   中英

Is there any way to avoid these $with models from loading whenever loading the parent in Laravel 5.7 Eloquent?

I have a laravel application in which I have Models using $with, in the frontend whenever I load this Model it loads its child's $with as well. Can you guys tell me how to avoid that?

Below is an example for you guys to understand the scenario.

ModelA.php
$with = [ModelK, ModelL, ModelM];
ModelK.php
$with = [ModelM, ModelN];
ModelM.php
$with = [ModelX, ModelY, ModelZ];

In my frontend, wherever I use ModelA with eloquent, it makes query on ModelM , ModelX both. This is making my site slow. Also if I retrieve 4-5 rows data from ModelA then it also make runs queries on all these models ModelM , ModelX and that too one query for each row. So the number of queries is also increase.

The relationship between these models are One-One, One-Many and Many-Many.

Is there any way to avoid these child Models whenever loading the parent?

If you are using the $with property in your models, you can disable the eagerLoading within a query with the following:

ModelA::setEagerLoads([])->first();

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM