Why not have a films table, a languages table and then a film_language pivot table with title, genre and description there?
How to bund a DB table to her model?
I'm doing myltilanguage project and need to do several tables for more of text. In doing so, I have a main table 'films' where have rows "title_en", "genre_en" and "description_en".
I needed to make 30 rows yet, for example "title_pl", "genre_pl", "title_fr" and etc. That is, to create another 30 columns for each language, which seemed very cumbersome to me and I decided to split it into 10 separate tables. An example table for French is called "fr_locals" or "it_locals". And now I have a problem how to bound these tables with their basic model Film (table "films"). Through the field "film_id".
Since I have already translated some of the databases into other languages, I have models and templates according to which everything works correctly.
Part of model Film:
protected $lang_fields = [
'title',
'genre',
'description',
];
public function getAttribute($key)
{
$default = parent::getAttribute($key);
if (isset($this->lang_fields) && is_array($this->lang_fields) && in_array($key, $this->lang_fields)) {
return $this->{$key . '_' . app()->getLocale() } ?? $this->{$key . '_' . 'en' };
}
return $default;
}
Controller:
public function changeLocale($locale)
{
$availableLocales = ['en', 'pl', 'ru', 'ua', 'de','fr', 'it', 'jp', 'pt', 'sp', 'tr'];
if (!in_array($locale, $availableLocales)) {
$locale = config('app.locale');
}
session(['locale' => $locale]);
App::setLocale($locale);
return redirect()->back();
}
The default language is English, when switching to another language, the reaction described in the code above occurs. The language translation works either with the helper in the templates, or with the help of the code described in the Movie model. Unfortunately, I do not know how to bind my main model to additional tables so that when switching the language, information from the additional table is picked up, and not the default language is displayed.
How to do this, how to join the database table for example "fr_locals" to the table "films" by the fields film_id<->id ?
Why not create a single table with columns film_id, language, title, genre, description? Then you can create the HasMany relationship and so you can have as many languages you want without needing to create new columns/tables everytime you want to add a new language.
Please or to participate in this conversation.