PHPackages                             roi-up-agency/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. roi-up-agency/laravel-translatable

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

roi-up-agency/laravel-translatable
==================================

A trait to make an Eloquent model hold translations

4.2.2(6y ago)026MITPHPPHP ^7.1

Since Apr 8Pushed 6y agoCompare

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

READMEChangelog (1)Dependencies (5)Versions (36)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)[![MIT Licensed](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'
```

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

[](#installation)

You can install the package via composer:

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

If you want to have another fallback\_locale then the app fallback locale (see `config/app.php`), you could 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`.

#### Getting all translations

[](#getting-all-translations)

You can get all translations by calling `getTranslations()` without an argument:

```
$newsItem->getTranslations();
```

Or you can use the accessor

```
$yourModel->translations
```

#### Setting a translation

[](#setting-a-translation)

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

```
$newsItem->name = 'New translation';
```

To set a translation for a specific locale you can use this method:

```
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();
```

#### Validation

[](#validation)

- if you want to validate an attribute for uniqueness before saving/updating the db, you might want to have a look at [laravel-unique-translation](https://github.com/codezero-be/laravel-unique-translation) which is made specifically for *laravel-translatable*.

#### 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();
```

Changelog
---------

[](#changelog)

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

Upgrading
---------

[](#upgrading)

### From v2 to v3

[](#from-v2-to-v3)

In most cases you can upgrade without making any changes to your codebase at all. `v3` introduced a `translations` accessor on your models. If you already had one defined on your model, you'll need to rename it.

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.

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

[](#postcardware)

You're free to use this package, but if it makes it to your production environment we highly appreciate you 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.

We publish all received postcards [on our company website](https://spatie.be/en/opensource/postcards).

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 multilingual package](https://github.com/themsaid/laravel-multilingual) were used in this readme.

Support us
----------

[](#support-us)

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).

Does your business depend on our contributions? Reach out and support us on [Patreon](https://www.patreon.com/spatie). All pledges will be dedicated to allocating workforce on maintenance and new awesome stuff.

License
-------

[](#license)

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

###  Health Score

31

—

LowBetter than 68% of packages

Maintenance20

Infrequent updates — may be unmaintained

Popularity6

Limited adoption so far

Community19

Small or concentrated contributor base

Maturity70

Established project with proven stability

 Bus Factor1

Top contributor holds 71.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 ~40 days

Total

34

Last Release

2350d ago

Major Versions

0.0.1 → 1.0.02016-04-11

1.3.0 → 2.0.02017-08-30

2.2.1 → 3.0.02018-10-16

3.1.3 → 4.0.02019-02-27

PHP version history (3 changes)0.0.1PHP ^7.0

3.0.0PHP ^7.1

4.1.0PHP ^7.2

### Community

Maintainers

![](https://www.gravatar.com/avatar/52db86196e778f12e6aa5b8e4803052700bcf08dae3dfbb6ee957c4209fcb2e1?d=identicon)[jsolam](/maintainers/jsolam)

---

Top Contributors

[![freekmurze](https://avatars.githubusercontent.com/u/483853?v=4)](https://github.com/freekmurze "freekmurze (112 commits)")[![sebastiandedeyne](https://avatars.githubusercontent.com/u/1561079?v=4)](https://github.com/sebastiandedeyne "sebastiandedeyne (10 commits)")[![ankurk91](https://avatars.githubusercontent.com/u/6111524?v=4)](https://github.com/ankurk91 "ankurk91 (2 commits)")[![miguelgracia](https://avatars.githubusercontent.com/u/3466864?v=4)](https://github.com/miguelgracia "miguelgracia (2 commits)")[![royduin](https://avatars.githubusercontent.com/u/1703233?v=4)](https://github.com/royduin "royduin (2 commits)")[![nateritter](https://avatars.githubusercontent.com/u/198798?v=4)](https://github.com/nateritter "nateritter (2 commits)")[![ItsRD](https://avatars.githubusercontent.com/u/4502205?v=4)](https://github.com/ItsRD "ItsRD (2 commits)")[![alexjoffroy](https://avatars.githubusercontent.com/u/7209163?v=4)](https://github.com/alexjoffroy "alexjoffroy (2 commits)")[![ctf0](https://avatars.githubusercontent.com/u/7388088?v=4)](https://github.com/ctf0 "ctf0 (2 commits)")[![IlCallo](https://avatars.githubusercontent.com/u/10036108?v=4)](https://github.com/IlCallo "IlCallo (1 commits)")[![jorenvanhee](https://avatars.githubusercontent.com/u/231202?v=4)](https://github.com/jorenvanhee "jorenvanhee (1 commits)")[![jpmaga](https://avatars.githubusercontent.com/u/31849900?v=4)](https://github.com/jpmaga "jpmaga (1 commits)")[![mrmonat](https://avatars.githubusercontent.com/u/17597850?v=4)](https://github.com/mrmonat "mrmonat (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)")[![robinmartini](https://avatars.githubusercontent.com/u/1028473?v=4)](https://github.com/robinmartini "robinmartini (1 commits)")[![RobLui](https://avatars.githubusercontent.com/u/10846766?v=4)](https://github.com/RobLui "RobLui (1 commits)")[![RonMelkhior](https://avatars.githubusercontent.com/u/1017721?v=4)](https://github.com/RonMelkhior "RonMelkhior (1 commits)")[![tabacitu](https://avatars.githubusercontent.com/u/1032474?v=4)](https://github.com/tabacitu "tabacitu (1 commits)")[![tdgroot](https://avatars.githubusercontent.com/u/1165302?v=4)](https://github.com/tdgroot "tdgroot (1 commits)")

---

Tags

spatietranslatemodeleloquentmultilinguallaravel-translatablei8n

###  Code Quality

TestsPHPUnit

### Embed Badge

![Health badge](/badges/roi-up-agency-laravel-translatable/health.svg)

```
[![Health](https://phpackages.com/badges/roi-up-agency-laravel-translatable/health.svg)](https://phpackages.com/packages/roi-up-agency-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)
