Package to help you easily translate your laravel models

0.1.0 2016-10-03 14:04 UTC

This package is auto-updated.

Last update: 2021-10-01 00:16:47 UTC


StyleCI Status Software License Latest Stable Version Total Downloads

Easily translate your laravel models to as many languages you need.


Add this line to your composer.json file:

    "escapework/laravel-translations": "0.2.*"

And add this service provider to your laravel providers:


And publish the migrations running the following command:

$ php artisan vendor:publish --provider="EscapeWork\Translations\TranslationServiceProvider"
$ php artisan migrate


Creating the locales

First, you need to create the locales that your models will be translated.

EscapeWork\Translations\Locale::create(['id' => 'pt-br', 'title' => 'Português (Brasil)']);
EscapeWork\Translations\Locale::create(['id' => 'en',    'title' => 'English']);

Then, you need to import the Translatable in your models.

use EscapeWork\Translations\Translatable;

class Product extends Model

    use Translatable;

Storing a translation

For storing a translation, you can do the following:

// $data can have as many fields you want
$data = [
    'title'       => 'My translated title',
    'description' => 'Lorem ipsum dolor sit amet, consectetur adipisicing elit',

$product->storeTranslation((array) $data, 'pt-br');

Here it's an example on how you can use with Laravel $request object.

$product = Product::find(1);

foreach ((array) $request->translations as $locale => $data) {
    $product->storeTranslation((array) $data, $locale);

Deleting translations from a model


Getting a translation

For getting an existing translation, you just need to do this:

$product = Product::find(1);
echo $product->translations->_get('title'); // this will get the translation for the current config('app.locale') value

If you need an translation for an specific locale, just pass the locale as the second argument:

$product = Product::find(1);
echo $product->translations->_get('title', 'pt-br');

If you don't pass the $locale, the default is gonna be the config('app.locale') value.

You can also do something like this to make your life easier:

class Product extends Model
    public function getTitleAttribute()
        return $this->translations->_get('title');

// then, just call like a simple field
echo $product->title;

Next steps

  • Testing;
  • Make use of MySQL 5.7 JSON types, where a search inside the translations will be available;


See the License file.