laravel-json-api / eloquent

Serialize Eloquent models to JSON API resources
MIT License
12 stars 15 forks source link

Avoid unnecessary query in ModelLoader #29

Closed aimeos closed 1 year ago

aimeos commented 1 year ago

For queries to a single resource (e.g. v1/pages/1 ) for which no relations should be fetched, there's an unnecessary query executed:

select "id" from "pages" where "pages"."id" in (1)

This is because $this->queryParameters->countable() returns NULL here: https://github.com/laravel-json-api/eloquent/blob/develop/src/QueryOne.php#L125

The patch checks for NULL values and avoids that query in this case.

aimeos commented 1 year ago

Thanks, replaced the code with your suggestion.

aimeos commented 1 year ago

@lindyhopchris Can you merge the PR now?

lindyhopchris commented 1 year ago

Sorry, thanks for the prompt! Yeah will merge and tag this weekend.

lindyhopchris commented 1 year ago

Apologies this took a while. Tagged as v3.0.1. Thanks for your contribution!