Support the ongoing development of →
Database Eloquent
Last updated 10 months ago.

Given the amount of data (unless your app is very text heavy) It can be easier / better / quicker to get all the text for that language on load and then a simple getter to that array of information.

E.g. on load (base controller or whatever) get the entire language and store in an array/object.

Then get the text required by ID to that array/object This can be further optimised by caching this store - as a local store object. (e.g. not loaded on subsequent visits by the user)

Means one longer init query, but post that, no impact on the system. Given that a page could use 50 text translations it can be very beneficial.

This is a problem, for example opencart suffers from on something very similar. Front loading it all makes even a very light text page more efficient overall.

Again depends on your app - but worth considering.

Last updated 10 months ago.

Thank MatyHaty for your answer. Unfortunately the app will be very text heavy. That is what we 'sell'. For now I created an extra method in my model:

public function translate($fieldName) {
	return $this->newCollection((array)$this->hasMany('Translation', 'translatable_id', $fieldName)

The Collection method does the following:

public function inLanguage()
	$models = $this;

	$returnArray = [];	

	foreach($models as $key => $model) {
		foreach($model as $languages) {
			$returnArray['to' . ucfirst(strtolower($languages->language->english_name))] = $languages->translation;
			$returnArray[strtolower($languages->language->abbreviation)] = $languages->translation;

	return (object) $returnArray;

So in my controller I can do the following:


This took the amount of queries back to three.

  1. Select Category
  2. Select Translatables/Translations (in this case description)
  3. Select Language Meta data.

Downside of this method is that i have to re-create this method over and over for every model I want to make translatable. And when I need to collect multiple field (eg: product name, product short description, product description, et cetera) I drastically increase the amount of SQL-queries. Probably need to load the language meta data on app start up (as you suggest), but I am still having the problem of not being able to eager load a whole models translations.

Maybe you have an idea for this?

Thanks in advance!

Last updated 10 months ago.

Here is an different approach on translating Eloquent objects:

Last updated 10 months ago.

Hello @deargonaut,

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 where all the translations will be stored...

  • 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 is usefull... Cheers!

Last updated 7 years ago.

Sign in to participate in this thread!

Full Stack Europe

Your banner here too?


We'd like to thank these amazing companies for supporting us

Your logo here?

The Laravel portal for problem solving, knowledge sharing and community building.

© 2023 - All rights reserved.