Closed MohammedYAmmar closed 5 years ago
Hey,
you should hide the key via one of the described ways in the laravel docs like following:
Category::all()->makeHidden('translations');
thanks it works But Is there any way to shorten this code
$categories = Category::all()->makeHidden('translations');
foreach ($categories as $category) {
$category->setDefaultLocale($request->header('lang'));
}
You can set the translatable.locale
config or change the app locale. These will set the locale for all translation models for the current runtime.
In your case I would set the app locale in a middleware. This will remove this logic from the controllers and run it for all requests.
i have used this
App::setLocale($request->header('lang'));
but it's not work
What means "not work"? What's in your $request->header('lang')
? What does app('translatable.locales')->current()
return? Do you call it before the category query (you have to do so) or after it?
it not translate to the locale i send from the header
and i have done this but it is a still in the return the default locale in the config file
<?php
namespace App\Http\Middleware;
use Closure;
class SetLocale
{
/**
* Handle an incoming request.
*
* @param \Illuminate\Http\Request $request
* @param \Closure $next
* @return mixed
*/
public function handle($request, Closure $next)
{
\App::setLocale($request->header('lang'));
return $next($request);
}
}
If you've set a locale in config translatable.locale
this won't be overridden. But you don't have to set it. It's just if you want to handle this packages locale separated from the app. In this case you can use config('translatable.locale', $request->header('lang'))
to change it.
As long as the config returns something we won't read the translator/app locale to get the current one. https://github.com/Astrotomic/laravel-translatable/blob/8ec38419ef538e1e973dace59503299462dfca05/src/Translatable/Locales.php#L46-L49
thank you so much it work now
I want it to return
for all the rows not like this
with out translation object