My team at Spatie is currenlty building Mailcoach, a solution to self host your e-mail newsletter. Mailcoach can be used a stand alone software or as a Laravel package. Subscribe now at Mailcoach to get a notification as soon as we release it.

Making Eloquent models translatable

Original – by Freek Van der Herten – 2 minute read

Spatie, the company where I work, is located in Belgium. Although our country is quite small, there are three official languages: Dutch, French and German. That's why nearly all of our projects are multilingual. In most projects we used to rely on Dimitris Savvopoulos popular translatable package to make Eloquent models translatable. Installing and using it is fairly easy. A downside is that all translations are stored in a separate tables. This means that you need to migrate two tables and manage two models.

Today we released our newest package laravel-translatable. This big difference with Dimitris' package is that translations are stored as json. There is no need for a second table or model. The idea to store translations as json is not ours, we got it from active community member Mohamed Said (be sure to check out his blog).

To make a model translatable you just have to use the HasTranslations-trait and specify in a property called $translatable which attributes should be translatable. All translatable attributes should be mapped as a text-column in the database.

Here's an example of a model which uses HasTranslations-trait.

use Illuminate\Database\Eloquent\Model;
use Spatie\Translatable\HasTranslations;

class NewsItem extends Model
{
    use HasTranslations;

    public $translatable = ['name'];
}

Once that's done you can do this to work with translations:

$newsItem = new NewsItem; // This is an Eloquent model
$newsItem
   ->setTranslation('name', 'en', 'Name in English');
   ->setTranslation('name', 'nl', 'Naam in het Nederlands');
   ->save();

$newsItem->name; // Returns 'Name in English' given that the current app locale is 'en'
$newsItem->getTranslation('name', 'nl'); // returns 'Naam in het Nederlands'

app()->setLocale('nl');

$newsItem->name; // Returns 'Naam in het Nederlands'

As announced in Mohamed's post Laravel 5.2.23 gained fluent syntax to query json columns. If you use MySQL >= 5.7 and Laravel 5.2.23 you can do this:

NewsItem::where('name->en', 'Name in English')->get();

Awesome right?

Take a look at the package on GitHub to learn all available methods. If you like the package, take a look at the other ones we've previously made.

Stay up to date with all things Laravel, PHP, and JavaScript.

Follow me on Twitter. I regularly tweet out programming tips, and what I myself have learned in ongoing projects.

Every two weeks I send out a newsletter containing lots of interesting stuff for the modern PHP developer.

Expect quick tips & tricks, interesting tutorials, opinions and packages. Because I work with Laravel every day there is an emphasis on that framework.

Rest assured that I will only use your email address to send you the newsletter and will not use it for any other purposes.

Nemanja Ivankovic liked on 10th July 2019
php[world] retweeted on 10th July 2019
Shashank Kakrecha liked on 10th July 2019
GlowLogix liked on 10th July 2019
Davi Gomes liked on 10th July 2019
Scorp974 liked on 10th July 2019
Andrés Herrera García liked on 10th July 2019
Sachin Patil retweeted on 10th July 2019
Sachin Patil liked on 10th July 2019
Daniel Schmitz liked on 10th July 2019
Duy Ha Nguyen liked on 10th July 2019
Mostafa Hosseini liked on 10th July 2019
Lorenc liked on 10th July 2019
Lucas Fiege liked on 10th July 2019
Mike liked on 10th July 2019
Chun liked on 10th July 2019
Albert liked on 10th July 2019
Tiagosimoes liked on 9th July 2019
Maarten Buis liked on 9th July 2019
Bill Yanelli liked on 9th July 2019
Madalin Tache liked on 9th July 2019
Lars Schou 🇩🇰 liked on 9th July 2019
Vitor Hugo R liked on 9th July 2019
Max liked on 9th July 2019
Sam D Harris liked on 9th July 2019
ArielSalvadorDev retweeted on 9th July 2019
ArielSalvadorDev liked on 9th July 2019
Twelfth liked on 9th July 2019
Leonel Elimpe liked on 9th July 2019
Carlos Moran liked on 9th July 2019
Diego Orellana liked on 9th July 2019
Scott Dutton liked on 9th July 2019
Nikunj Kabariya liked on 9th July 2019
Amir Ahmic liked on 9th July 2019
Iftekher Sunny liked on 9th July 2019