An issue with Mutators when API request
ahsys-app opened this issue · comments
When using API service, I want to show the translation value for the keys whois have multi-translation activated.
The problem is the request starts to load and comes back with 500 Internal Server Error :
PHP Fatal error: Allowed memory size of 536870912 bytes exhausted
I fix that by adding this @ [ Line 83 ~ src/HasTranslations.php ]:
if (!request()->is('api/*') && $this->hasGetMutator($key)) {
return $this->mutateAttribute($key, $translation);
}
Instead of
if ($this->hasGetMutator($key)) {
return $this->mutateAttribute($key, $translation);
}
I don't know yet what is the issue but I fix it like that 👍
The result :
Model code :
public $translatable = ['name'];
public function getNameAttribute() {
return $this->getTranslation('name', app()->getLocale());
}
Tested with multi relations and work fine.