Closed jltrevizon closed 3 months ago
I have found the problem, it is in the protected property $with = [“translation”]; in ModelTranslation. I was using it this way to avoid loading unnecessary translations. Now my problem is, how can I return in the API response only the translation that the user requests in the request without having to show all the translation relationships?
Now my problem is, how can I return in the API response only the translation that the user requests in the request without having to show all the translation relationships?
You can hide the translations
attribute either by using the $hidden
attribute inside your Access model, for example:
protected $hidden = ["translations"];
Or maybe you can use the without()
method on your model to prevent the translations
relationship from loading:
Accessory::without("translations")->first();
But in my opinion, the best option would be to use an API Resource since you can get better control over the response form. There, you can choose to not send translations inside the response.
Of course, you can use a middleware to read the user locale and set it inside the application using a global middleware. Here's an example implementation that read the value of the Accept-Language
header and adjusts the locale of the application:
class SetLocale
{
public function handle(Request $request, Closure $next): Response
{
if ($request->hasHeader('Accept-Language') && !empty($request->header('Accept-Language'))) {
$locale = $request->header('Accept-Language');
$language = Locale::acceptFromHttp($locale);
App::setLocale($language);
}
return $next($request);
}
}
This issue is stale because it has been open 21 days with no activity. Remove stale label or comment or this will be closed in 7 days
Describe the bug Doesn´t save model translations.
To Reproduce Config Translatable:![imagen](https://github.com/Astrotomic/laravel-translatable/assets/22945459/23d42616-4868-495c-bf22-10f84a6065fb)
Create ModelTranslation![imagen](https://github.com/Astrotomic/laravel-translatable/assets/22945459/5bc09b60-2bb2-43fe-b0cf-963c5d1bcca1)
Configure Model with Translatable ;![imagen](https://github.com/Astrotomic/laravel-translatable/assets/22945459/4a1b735d-98a7-498f-a50e-9300c0d20e2b)
populate translation table:![imagen](https://github.com/Astrotomic/laravel-translatable/assets/22945459/e9ec4808-4f6d-416c-be9b-27ab5db40341)
Screenshots Testing using tinker![imagen](https://github.com/Astrotomic/laravel-translatable/assets/22945459/751316ea-b051-4edd-ac0e-7d61415cbe6b)
Versions (please complete the following information)