Astrotomic / laravel-translatable

A Laravel package for multilingual models
https://docs.astrotomic.info/laravel-translatable/
MIT License
1.22k stars 152 forks source link

Unload relationship after autoload for toArray() #412

Closed beschoenen closed 1 week ago

beschoenen commented 3 weeks ago

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 commented 1 week ago

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.