Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

Adriatik's avatar

Eloquent with database multi language - best practices

I would like to handle a database multi language like this with Eloquent:

Table "Product":
----------------
ID                 : int
<any other language-neutral fields>


Table "ProductTranslations"
---------------------------
ID                 : int      (foreign key referencing the Product)
Language           : varchar  (e.g. "en-US", "de-CH")
IsDefault          : bit
ProductDescription : nvarchar
<any other localized data>

Which is the most elegant way to do this with Eloquent?

0 likes
6 replies
unitedworx's avatar

With multi language there is no perfect way! It's a huge subject! You have to consider your options and follow what suits your use case the best way possible.

Trying to make your app multi lingual thought the database itself can become very very ugly especially if you are dealing with complex relationships and a lot of data. It's not only about storing the data but having the appropriate ui to see what's missing in each language and translate every single bit of it. Both data entered by users and the ui itself.

Over the past ten years that I have been dealing with client websites I dodged making our in-house cms multi lingual for the sole reason of keeping the code clean and manageable.

The first way I used was to simply have a different database for each language. So depending on the language I switched database. Same code base but different do for each language? Simple clean and no mess in my code!

The second way I used was to have a clone of the site in another domain and all info entered in new language. In both cases there were times that I had to transfer data from one version to the other with xml on a daily or hourly basis to keep them in sync.

It seems that you are tying to do it the third.. Erm hard way :) single db with all data. I think there are packages for eloquent to help you with this

https://github.com/dimsav/laravel-translatable

4 likes
OmarHossamEldinKandil's avatar

really ty im trying to do this bc i think it's much much cleaner for database design in my opinion

Adriatik's avatar
Adriatik
OP
Best Answer
Level 1

My solution is very similar to the package that you have found on github:

In the Product model:

public function translation($language = null)
 {
  if ($language == null) {
   $language = App::getLocale();
  }
  return $this->hasMany('ProductTranslations')->where('language', '=', $language);
 }

In the ProductTranslations model:

public function product()
 {
  return $this->belongsTo('Product');
 }

And then for use it

Product::find(2)->translation->first()->productDescription;

Or

Product::find(2)->translation('de')->get()->first()->productDescription;
2 likes
mendi19's avatar

@Adriatik This seems good approach but doesn't work when using it says: Property [translation] does not exist on the Eloquent builder instance. Any idea how to fix this!?

igaster's avatar

Hello,

I 've created a package igaster/laravel-translate-eloquent that adds translation to any Eloquent Model. It requires only one additional table in your DB for all the translations...

  • You can get/set a translated key as it was a normal Eloquent key (eg $article->title), when actually the correct translation will be looked up and returned from the Translations table...
  • Of course you can request a translation to any locale.
  • It supports a fallback locale if there is no translation in the current locale.

Hope it helps Cheers!

bayco's avatar

hello @adriatik, in what cases and how you are using IsDefault field in ProductTranslations table?

Please or to participate in this conversation.