Skip to content

Unload relationship after autoload for toArray() #412

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

Closed
wants to merge 1 commit into from

Conversation

beschoenen
Copy link

This makes sure that translations is removed from the model after toArray() is called, so it doesn't appear in any API responses.

This option is opt-in and therefor backwards compatible.

@Gummibeer
Copy link
Member

Hey,
thanks for your PR but as I strongly disagree with return $model->toArray() as a good way to build APIs and I would have to maintain that one over the next years I don't want to merge it in.
Laravel comes with a great way to define the array scheme for API responses: https://laravel.com/docs/11.x/eloquent-resources
And using these there's no problem in having the relationship loaded. In worst case that new behavior would also result in unwanted multipe queries as I probably call toArray() to transform my model in a DTO and after that access $model->title (translated attribute) directly which will again load the relationship as the toArray() has already loaded but immediately unset it.

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

Successfully merging this pull request may close these issues.

2 participants