diff --git a/changelog.md b/changelog.md index 70d033a..3557a1f 100644 --- a/changelog.md +++ b/changelog.md @@ -6,6 +6,7 @@ ``` $country->fill(['name:en' => 'Belgium']); ``` +- Added config to skip translations in toArray() for better performance when needed. #315 ### v. 6.0.1 diff --git a/readme.md b/readme.md index c88ef42..5dbef99 100644 --- a/readme.md +++ b/readme.md @@ -178,6 +178,10 @@ With this command, initialize the configuration and modify the created file, loc ## Configuration +### The config file + +You can see the options for further customization in the [config file](src/config/translatable.php). + ### The translation model The convention used to define the class of the translation model is to append the keyword `Translation`. diff --git a/src/Translatable/Translatable.php b/src/Translatable/Translatable.php index d043fa8..46e9936 100644 --- a/src/Translatable/Translatable.php +++ b/src/Translatable/Translatable.php @@ -586,6 +586,12 @@ public function toArray() { $attributes = parent::toArray(); + if ($this->relationLoaded('translations') || $this->toArrayAlwaysLoadsTranslations()) { + // continue + } else { + return $attributes; + } + $hiddenAttributes = $this->getHidden(); foreach ($this->translatedAttributes as $field) { @@ -651,4 +657,12 @@ private function getAttributeAndLocale($key) return [$key, $this->locale()]; } + + /** + * @return bool + */ + private function toArrayAlwaysLoadsTranslations() + { + return app()->make('config')->get('translatable.to_array_always_loads_translations', true); + } } diff --git a/src/config/translatable.php b/src/config/translatable.php index 3734d7e..14d47bf 100644 --- a/src/config/translatable.php +++ b/src/config/translatable.php @@ -91,4 +91,14 @@ */ 'locale_key' => 'locale', + /* + |-------------------------------------------------------------------------- + | Always load translations when converting to array + |-------------------------------------------------------------------------- + | Setting this to false will have a performance improvement but will + | not return the translations when using toArray(), unless the + | translations relationship is already loaded. + | + */ + 'to_array_always_loads_translations' => true, ]; diff --git a/tests/TranslatableTest.php b/tests/TranslatableTest.php index 1b7e1ed..f13833e 100644 --- a/tests/TranslatableTest.php +++ b/tests/TranslatableTest.php @@ -435,6 +435,20 @@ public function test_to_array_and_fallback_with_country_based_locales_enabled() $this->assertSame('frites', $fritesArray['name']); } + public function test_it_skips_translations_in_to_array_when_config_is_set() + { + $this->app->config->set('translatable.to_array_always_loads_translations', false); + $greece = Country::whereCode('gr')->first()->toArray(); + $this->assertFalse(isset($greece['name'])); + } + + public function test_it_returns_translations_in_to_array_when_config_is_set_but_translations_are_loaded() + { + $this->app->config->set('translatable.to_array_always_loads_translations', false); + $greece = Country::whereCode('gr')->with('translations')->first()->toArray(); + $this->assertTrue(isset($greece['name'])); + } + public function test_it_should_mutate_the_translated_attribute_if_a_mutator_is_set_on_model() { $person = new Person(['name' => 'john doe']);