Oh Dear is the all-in-one monitoring tool for your entire website. We monitor uptime, SSL certificates, broken links, scheduled tasks and more. You'll get a notifications for us when something's wrong. All that paired with a developer friendly API and kick-ass documentation. O, and you'll also be able to create a public status page under a minute. Start monitoring using our free trial now.

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.

Share Post LinkedIn

I write about Laravel, PHP, AI and building better software.

Every two weeks, I share practical tips, tutorials, and behind-the-scenes insights from maintaining 300+ open source packages. Join thousands of developers who read along.

No spam. Unsubscribe anytime. You can also follow me on X.

Found something interesting to share?

The community section is a place where developers share links to articles, tutorials and videos. Submit a link and help fellow developers discover great content. As a thank you, you'll receive a coupon for a discount on Spatie products.