PHPackages                             zoomyboy/laravel-translatable - PHPackages - PHPackages  [Skip to content](#main-content)[PHPackages](/)[Directory](/)[Categories](/categories)[Trending](/trending)[Leaderboard](/leaderboard)[Changelog](/changelog)[Analyze](/analyze)[Collections](/collections)[Log in](/login)[Sign up](/register)

1. [Directory](/)
2. /
3. [Database &amp; ORM](/categories/database)
4. /
5. zoomyboy/laravel-translatable

ActiveLibrary[Database &amp; ORM](/categories/database)

zoomyboy/laravel-translatable
=============================

A trait to make an Eloquent model hold translations

2.0.0(8y ago)113MITPHPPHP ^7.0

Since Apr 8Pushed 8y agoCompare

[ Source](https://github.com/zoomyboy/laravel-translatable)[ Packagist](https://packagist.org/packages/zoomyboy/laravel-translatable)[ Docs](https://github.com/spatie/laravel-translatable)[ RSS](/packages/zoomyboy-laravel-translatable/feed)WikiDiscussions master Synced today

READMEChangelogDependencies (5)Versions (16)Used By (0)

A trait to make Eloquent models translatable
============================================

[](#a-trait-to-make-eloquent-models-translatable)

[![Latest Version on Packagist](https://camo.githubusercontent.com/84243101d1a1e96438193f844bb47df7297d24f0a55c966c439a8c9c27d72ebd/68747470733a2f2f696d672e736869656c64732e696f2f7061636b61676973742f762f7370617469652f6c61726176656c2d7472616e736c617461626c652e7376673f7374796c653d666c61742d737175617265)](https://packagist.org/packages/spatie/laravel-translatable)[![Software License](https://camo.githubusercontent.com/55c0218c8f8009f06ad4ddae837ddd05301481fcf0dff8e0ed9dadda8780713e/68747470733a2f2f696d672e736869656c64732e696f2f62616467652f6c6963656e73652d4d49542d627269676874677265656e2e7376673f7374796c653d666c61742d737175617265)](LICENSE.md)[![Build Status](https://camo.githubusercontent.com/1fe301a36b44cc1e7b1f12f06908ad25c3e7274cd22777c832e7cf3958b96dba/68747470733a2f2f696d672e736869656c64732e696f2f7472617669732f7370617469652f6c61726176656c2d7472616e736c617461626c652f6d61737465722e7376673f7374796c653d666c61742d737175617265)](https://travis-ci.org/spatie/laravel-translatable)[![Quality Score](https://camo.githubusercontent.com/4c5af826f0269859188d69f3ee0c411318a1cf1c610e6d056b81c11aaa556b8f/68747470733a2f2f696d672e736869656c64732e696f2f7363727574696e697a65722f672f7370617469652f6c61726176656c2d7472616e736c617461626c652e7376673f7374796c653d666c61742d737175617265)](https://scrutinizer-ci.com/g/spatie/laravel-translatable)[![StyleCI](https://camo.githubusercontent.com/7c96715a72da82508feeeb6f9c3a8b1de01ee21c705858d5b20fd97241731a02/68747470733a2f2f7374796c6563692e696f2f7265706f732f35353639303434372f736869656c643f6272616e63683d6d6173746572)](https://styleci.io/repos/55690447)[![Total Downloads](https://camo.githubusercontent.com/cf6bb0932610b395888b6e428a7355558aa08ac7d93a9533d4eff5f06509470b/68747470733a2f2f696d672e736869656c64732e696f2f7061636b61676973742f64742f7370617469652f6c61726176656c2d7472616e736c617461626c652e7376673f7374796c653d666c61742d737175617265)](https://packagist.org/packages/spatie/laravel-translatable)

This package contains a trait to make Eloquent models translatable. Translations are stored as json. There is no extra table needed to hold them.

Once the trait is installed on the model you can do these things:

```
$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'
```

Postcardware
------------

[](#postcardware)

You're free to use this package (it's [MIT-licensed](LICENSE.md)), but if it makes it to your production environment we highly appreciate your sending us a postcard from your hometown, mentioning which of our package(s) you are using.

Our address is: Spatie, Samberstraat 69D, 2060 Antwerp, Belgium.

All postcards are published [on our website](https://spatie.be/en/opensource/postcards).

Installation
------------

[](#installation)

You can install the package via composer:

```
composer require spatie/laravel-translatable
```

The package will automatically register itself.

If you want to change add fallback\_locale, you must publish the config file:

```
php artisan vendor:publish --provider="Spatie\Translatable\TranslatableServiceProvider"

```

This is the contents of the published file:

```
return [
  'fallback_locale' => 'en',
];
```

Making a model translatable
---------------------------

[](#making-a-model-translatable)

The required steps to make a model translatable are:

- First you need to add the `Spatie\Translatable\HasTranslations`-trait.
- Next you should create a public property `$translatable` which holds an array with all the names of attributes you wish to make translatable.
- Finally you should make sure that all translatable attributes are set to the `text`-datatype in your database. If your database supports `json`-columns, use that.

Here's an example of a prepared model:

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

class NewsItem extends Model
{
    use HasTranslations;

    public $translatable = ['name'];
}
```

### Available methods

[](#available-methods)

#### Getting a translation

[](#getting-a-translation)

The easiest way to get a translation for the current locale is to just get the property for the translated attribute. For example (given that `name` is a translatable attribute):

```
$newsItem->name;
```

You can also use this method:

```
public function getTranslation(string $attributeName, string $locale) : string
```

This function has an alias named `translate`.

#### Setting a translation

[](#setting-a-translation)

```
public function setTranslation(string $attributeName, string $locale, string $value)
```

To actually save the translation, don't forget to save your model.

```
$newsItem->setTranslation('name', 'en', 'Updated name in English');

$newsItem->save();
```

#### Forgetting a translation

[](#forgetting-a-translation)

You can forget a translation for a specific field:

```
public function forgetTranslation(string $attributeName, string $locale)
```

You can forget all translations for a specific locale:

```
public function forgetAllTranslations(string $locale)
```

#### Getting all translations in one go

[](#getting-all-translations-in-one-go)

```
public function getTranslations(string $attributeName): array
```

#### Setting translations in one go

[](#setting-translations-in-one-go)

```
public function setTranslations(string $attributeName, array $translations)
```

Here's an example:

```
$translations = [
   'en' => 'Name in English',
   'nl' => 'Naam in het Nederlands'
];

$newsItem->setTranslations('name', $translations);
```

### Events

[](#events)

#### TranslationHasBeenSet

[](#translationhasbeenset)

Right after calling `setTranslation` the `Spatie\Translatable\Events\TranslationHasBeenSet`-event will be fired.

It has these properties:

```
/** @var \Illuminate\Database\Eloquent\Model */
public $model;

/** @var string  */
public $attributeName;

/** @var string  */
public $locale;

public $oldValue;
public $newValue;
```

### Creating models

[](#creating-models)

You can immediately set translations when creating a model. Here's an example:

```
NewsItem::create([
   'name' => [
      'en' => 'Name in English'
      'nl' => 'Naam in het Nederlands'
   ],
]);
```

### Querying translatable attributes

[](#querying-translatable-attributes)

If you're using MySQL 5.7 or above, it's recommended that you use the json data type for housing translations in the db. This will allow you to query these columns like this:

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

### Using translations in json responses

[](#using-translations-in-json-responses)

The easiest way to add translations to json reponse is to override the `toArray` method on your model.

Here's a quick example:

```
// in your model

    /**
     * Convert the model instance to an array.
     *
     * @return array
     */
    public function toArray()
    {
        $attributes = parent::toArray();

        foreach ($this->getTranslatableAttributes() as $name) {
            $attributes[$name] = $this->getTranslation($name, app()->getLocale());
        }

        return $attributes;
    }
}
```

Changelog
---------

[](#changelog)

Please see [CHANGELOG](CHANGELOG.md) for more information what has changed recently.

Testing
-------

[](#testing)

```
$ composer test
```

Contributing
------------

[](#contributing)

Please see [CONTRIBUTING](CONTRIBUTING.md) for details.

Security
--------

[](#security)

If you discover any security related issues, please email  instead of using the issue tracker.

Credits
-------

[](#credits)

- [Freek Van der Herten](https://github.com/freekmurze)
- [Sebastian De Deyne](https://github.com/sebastiandedeyne)
- [All Contributors](../../contributors)

We got the idea to store translations as json in a column from [Mohamed Said](https://github.com/themsaid). Parts of the readme of [his multiligual package](https://github.com/themsaid/laravel-multilingual) were used in this readme.

About Spatie
------------

[](#about-spatie)

Spatie is a webdesign agency based in Antwerp, Belgium. You'll find an overview of all our open source projects [on our website](https://spatie.be/opensource).

License
-------

[](#license)

The MIT License (MIT). Please see [License File](LICENSE.md) for more information.

###  Health Score

29

—

LowBetter than 60% of packages

Maintenance20

Infrequent updates — may be unmaintained

Popularity7

Limited adoption so far

Community14

Small or concentrated contributor base

Maturity66

Established project with proven stability

 Bus Factor1

Top contributor holds 81.3% of commits — single point of failure

How is this calculated?**Maintenance (25%)** — Last commit recency, latest release date, and issue-to-star ratio. Uses a 2-year decay window.

**Popularity (30%)** — Total and monthly downloads, GitHub stars, and forks. Logarithmic scaling prevents top-heavy scores.

**Community (15%)** — Contributors, dependents, forks, watchers, and maintainers. Measures real ecosystem engagement.

**Maturity (30%)** — Project age, version count, PHP version support, and release stability.

###  Release Activity

Cadence

Every ~44 days

Recently: every ~24 days

Total

13

Last Release

3158d ago

Major Versions

0.0.1 → 1.0.02016-04-11

1.3.0 → 2.0.02017-08-30

### Community

Maintainers

![](https://www.gravatar.com/avatar/51877391ce0d185caee343c177e5186bf05efb7d29c4ff411309be119dcb616c?d=identicon)[zoomyboy](/maintainers/zoomyboy)

---

Top Contributors

[![freekmurze](https://avatars.githubusercontent.com/u/483853?v=4)](https://github.com/freekmurze "freekmurze (74 commits)")[![sebastiandedeyne](https://avatars.githubusercontent.com/u/1561079?v=4)](https://github.com/sebastiandedeyne "sebastiandedeyne (7 commits)")[![ItsRD](https://avatars.githubusercontent.com/u/4502205?v=4)](https://github.com/ItsRD "ItsRD (2 commits)")[![jorenvanhee](https://avatars.githubusercontent.com/u/231202?v=4)](https://github.com/jorenvanhee "jorenvanhee (1 commits)")[![meta0102](https://avatars.githubusercontent.com/u/14222016?v=4)](https://github.com/meta0102 "meta0102 (1 commits)")[![Omranic](https://avatars.githubusercontent.com/u/406705?v=4)](https://github.com/Omranic "Omranic (1 commits)")[![Propaganistas](https://avatars.githubusercontent.com/u/6680176?v=4)](https://github.com/Propaganistas "Propaganistas (1 commits)")[![ctf0](https://avatars.githubusercontent.com/u/7388088?v=4)](https://github.com/ctf0 "ctf0 (1 commits)")[![vmitchell85](https://avatars.githubusercontent.com/u/1248035?v=4)](https://github.com/vmitchell85 "vmitchell85 (1 commits)")[![drbyte](https://avatars.githubusercontent.com/u/404472?v=4)](https://github.com/drbyte "drbyte (1 commits)")[![IlCallo](https://avatars.githubusercontent.com/u/10036108?v=4)](https://github.com/IlCallo "IlCallo (1 commits)")

---

Tags

spatietranslatemodeleloquentmultilinguallaravel-translatablei8n

###  Code Quality

TestsPHPUnit

### Embed Badge

![Health badge](/badges/zoomyboy-laravel-translatable/health.svg)

```
[![Health](https://phpackages.com/badges/zoomyboy-laravel-translatable/health.svg)](https://phpackages.com/packages/zoomyboy-laravel-translatable)
```

###  Alternatives

[spatie/laravel-translatable

A trait to make an Eloquent model hold translations

2.4k23.0M413](/packages/spatie-laravel-translatable)[mongodb/laravel-mongodb

A MongoDB based Eloquent model and Query builder for Laravel

7.1k7.2M71](/packages/mongodb-laravel-mongodb)[tucker-eric/eloquentfilter

An Eloquent way to filter Eloquent Models

1.8k4.8M26](/packages/tucker-eric-eloquentfilter)[dyrynda/laravel-model-uuid

This package allows you to easily work with UUIDs in your Laravel models.

4802.8M8](/packages/dyrynda-laravel-model-uuid)[spiritix/lada-cache

A Redis based, automated and scalable database caching layer for Laravel

591444.8k2](/packages/spiritix-lada-cache)[pdphilip/elasticsearch

An Elasticsearch implementation of Laravel's Eloquent ORM

145360.2k4](/packages/pdphilip-elasticsearch)

PHPackages © 2026

[Directory](/)[Categories](/categories)[Trending](/trending)[Changelog](/changelog)[Analyze](/analyze)
