-
-
Notifications
You must be signed in to change notification settings - Fork 484
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Eager loading includes
#670
Comments
Note to anyone who is interested to implement this: In here search for Eager-Loading vs Lazy-Loading. |
I use an extra method in all Tasks that require eager-loading and call it before the run();
in Action I call
Maybe this helps.. |
In Task
|
I have something like that as well on task layer, even with include-relation mapping, if the two dont match, but I think its not very clean and elegant and it does not work for nested includes. |
I haven't tested the following, but I think another option you can use is the query parameter ?l5_with=company;developer The semicolon is used as the separator. |
Hi. I've reading this thread.. is posible to tell a Transformer class which relationship(s) are eager-loaded? In Laravel API Resources there are many ways to tell a Resource which relationship(s) are eager-loaded. This is an example from laravel documentation.
|
I think for now we need to do a work around. I created a criteria to load relationships first. class WithRelationshipsCriteria extends Criteria
{
public function __construct(private readonly array $relationships = []) {
}
public function apply($model, RepositoryInterface $repository) {
$requestIncludes = [];
if (app('request')->has('include')) {
$include = app('request')->get('include');
$requestIncludes = explode(',', $include);
}
return $model->with(
array_merge($this->relationships, $requestIncludes)
);
}
} In the public function boot() {
parent::boot();
$this->pushCriteria(WithRelationshipsCriteria::class);
} Since all repositories base off this parent then eager loading relations will happen when relationships are requested |
@innoflash This is a clever workaround 😎 |
This only works where the public function boot(): void
{
$this->addRequestCriteria();
} |
I remember testing this and it did not work, so maybe I forgot to add the (convoluted) But then again, I dont want FE to have control over eager loading or not. |
Description:
When we are including relations in Apiato, the relationship data is
lazy loaded
. This means the relationship data is loaded at theTransporter include method
. This will cause a n+1 problem on get all rotes, because there is a new query for every parent model entity. This is not the desired behaviour in this case and the prefered methodwould beeager loading
the data before hitting theTransporter
. Whileeager loading
, the relationship data is loaded in a single query after the parent model data is queried, resulting in much less queries especially with many includes and more so with nested includes.Expected Behavior:
Maybe add an option to choose between
eager loading
andlazy loading
the includes?Additional Context:
Versions:
Apiato Version: master branch (v11)
PHP Version: 8.1.0
Steps To Reproduce:
include a relation
The text was updated successfully, but these errors were encountered: