Skip to content
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

N+1 query problem #252

Closed
bitskina opened this issue Oct 6, 2021 · 2 comments
Closed

N+1 query problem #252

bitskina opened this issue Oct 6, 2021 · 2 comments
Assignees
Labels

Comments

@bitskina
Copy link

bitskina commented Oct 6, 2021

When I need to select non translatable fields for example only id and call Model::all(['id']) it still tries to load translations and runs N+1 query.
image
image

@bitskina bitskina added the bug label Oct 6, 2021
@Gummibeer Gummibeer added question and removed bug labels Oct 6, 2021
@Gummibeer Gummibeer self-assigned this Oct 6, 2021
@Gummibeer
Copy link
Member

Hey,
first of all you can call with('translations') to prevent any n+1 query problem.
Secondly You should use Model::query()->pluck('id') if you only need a single column.
Next is that returning a model collection as response and using the models implicit array casting is bad practice. You should always use explicit resource classes for API/JSON responses: https://laravel.com/docs/8.x/eloquent-resources

And in case you need multiple columns which aren't translated and you haven't enabled loaded the translations it will not load them with explicit mapping as long as you don't use any translated attribute.

Your implicit array problem is likely an unwanted configuration on your end:

'to_array_always_loads_translations' => true,

@bitskina
Copy link
Author

bitskina commented Oct 6, 2021

Ok, thanks. It was just an example screens to describe a problem.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Development

No branches or pull requests

2 participants