Multilanguage database management with Laravel Multilanguage database management with Laravel laravel laravel

Multilanguage database management with Laravel


You will need to write that on your own. First you will have to model your database tables to support multilanguage content and then in your model you will be able to say something like:

class Content extends Eloquent{   public function scopeEnglish($query)   {          return $query->where('language', '=', 'en');   }   public function scopeSpanish($query)   {      return $query->where('language', '=', 'es');   }}class Post extends Eloquent{  public function content()  {      return $this->hasMany('Content');   }}

and then you can use it like:

$englishContent = Posts::find($id)->content->english()->get();$spanishContent = Posts::find($id)->content->spanish()->get();


Glad To Help's answer seems perfect for multilingual site's with many languages.I've found out that it's kind of clunky to do that when your site has just two/three languages.

What I've done is having two columns for each translatable fields.for the title attribute I have in the database title_en and title_es.After that, in the controller just set this method to do an automated translation.

public function getTitleAttribute(){    $locale = App::getLocale();    $column = "title_" . $locale;    return $this->{$column};}

Now when you call Post::find($id)->title it will automatically get the one for the current language.

Hope it helps.Cheers!


I did similar but more universal

Schema::create('index', function(Blueprint $table) {        $table->increments('id');        $table->string('title_uk');        $table->string('title_ru');        $table->string('title_en');        $table->string('heading_uk');        $table->string('heading_ru');        $table->string('heading_en');        $table->string('photo');        $table->timestamps();    });

The model

public function TextTrans($text){    $locale=App::getLocale();    $column=$text.'_'.$locale;    return $this->{$column};}

Now I for each language version as well as for each field will not prescribe a specific function, and cause all this:

$text=Index::find('1');$text->TextTrans('title');$text->TextTrans('heading');