Show records related to app locale setting.
wghughes95 opened this issue · comments
Is it possible to pull and display records stored in the database that is related to the app's locale setting.
i.e. I have a table of articles in different languages, so some are in English but not French and vice versa. When my app is set to English I would like my articles view to display the English articles only, when in French the French articles only etc.
public function index()
{
$articles = Article::all();
$locale = \App::getLocale();
foreach($articles as $article)
{
$object = $article->getTranslations('title');
if(isset($object[$locale]))
$goodArticles[] = $article;
}
return view('articles.index')->with([
'articles' => $goodArticles,
]);
}
The above code is finding articles which are in the app's current locale by checking if the $object has a title in that locale. If an article exists in that locale it will be returned, if it doesn't it will not be returned.
For now I have this temporary fix, which will work for a small number of values in the databse. However, as the database gets bigger (there will be thousands of entries eventually) this will become a problem as it will have to run this loop every time, ultimately slowing down the app.
This is not handled by the package.
I would add a scope named hasTranslations(string $locale)
to my project and add manipulate the query there to only return records of the given locale.