PHPackages                             solutionforest/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. solutionforest/laravel-translatable

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

solutionforest/laravel-translatable
===================================

A trait to make an Eloquent model hold translations

6.0.1(2y ago)04852MITPHPPHP ^8.0

Since Apr 8Pushed 2y agoCompare

[ Source](https://github.com/solutionforest/laravel-translatable)[ Packagist](https://packagist.org/packages/solutionforest/laravel-translatable)[ Docs](https://github.com/solutionforest/laravel-translatable)[ Patreon](https://www.patreon.com/solutionforest)[ RSS](/packages/solutionforest-laravel-translatable/feed)WikiDiscussions master Synced 3d ago

READMEChangelog (10)Dependencies (5)Versions (60)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/0696e96d91e1092a9bd9b196977c84b58e5c11af0d1278585ffa6a14d25e4a22/68747470733a2f2f696d672e736869656c64732e696f2f7061636b61676973742f762f736f6c7574696f6e666f726573742f6c61726176656c2d7472616e736c617461626c652e7376673f7374796c653d666c61742d737175617265)](https://packagist.org/packages/solutionforest/laravel-translatable)[![MIT Licensed](https://camo.githubusercontent.com/55c0218c8f8009f06ad4ddae837ddd05301481fcf0dff8e0ed9dadda8780713e/68747470733a2f2f696d672e736869656c64732e696f2f62616467652f6c6963656e73652d4d49542d627269676874677265656e2e7376673f7374796c653d666c61742d737175617265)](LICENSE.md)[![Total Downloads](https://camo.githubusercontent.com/cf6bb0932610b395888b6e428a7355558aa08ac7d93a9533d4eff5f06509470b/68747470733a2f2f696d672e736869656c64732e696f2f7061636b61676973742f64742f7370617469652f6c61726176656c2d7472616e736c617461626c652e7376673f7374796c653d666c61742d737175617265)](https://packagist.org/packages/solutionforest/laravel-translatable)

This package contains a trait to make Eloquent models translatable. Translations are stored in Database. All translation will be cached by default Laravel Cache.

This Library forked from [spatie/laravel-translatable](https://github.com/spatie/laravel-translatable)
------------------------------------------------------------------------------------------------------

[](#this-library-forked-from-spatielaravel-translatable)

Technically, all methods are same. Use database as storage only.

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 solutionforest/laravel-translatable
```

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

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

The required steps to make a model translatable are:

- First, `php artisian migrate` migrate the table
- Next, you need to add the `SolutionForest\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.

Here's an example of a prepared model:

```
use Illuminate\Database\Eloquent\Model;
use SolutionForest\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 `SolutionForest\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'
   ],
]);
```

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

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

[](#support-us)

SolutionForest is a solution house based in Hong Kong.[on our website](https://solutionforest.net).

License
-------

[](#license)

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

###  Health Score

37

—

LowBetter than 81% of packages

Maintenance20

Infrequent updates — may be unmaintained

Popularity14

Limited adoption so far

Community21

Small or concentrated contributor base

Maturity82

Battle-tested with a long release history

 Bus Factor1

Top contributor holds 58.2% 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 ~47 days

Recently: every ~216 days

Total

58

Last Release

1042d ago

Major Versions

1.3.0 → 2.0.02017-08-30

2.2.1 → 3.0.02018-10-16

3.1.3 → 4.0.02019-02-27

4.1.3 → 5.0.02019-07-18

5.4.0 → 6.0.12023-08-25

PHP version history (5 changes)0.0.1PHP ^7.0

3.0.0PHP ^7.1

4.1.0PHP ^7.2

5.3PHP ^7.2|^8.0

6.0.1PHP ^8.0

### Community

Maintainers

![](https://avatars.githubusercontent.com/u/68211796?v=4)[Solution Forest](/maintainers/solutionforest)[@solutionforest](https://github.com/solutionforest)

---

Top Contributors

[![freekmurze](https://avatars.githubusercontent.com/u/483853?v=4)](https://github.com/freekmurze "freekmurze (110 commits)")[![solutionforestteam](https://avatars.githubusercontent.com/u/53035878?v=4)](https://github.com/solutionforestteam "solutionforestteam (35 commits)")[![sebastiandedeyne](https://avatars.githubusercontent.com/u/1561079?v=4)](https://github.com/sebastiandedeyne "sebastiandedeyne (10 commits)")[![lam0819](https://avatars.githubusercontent.com/u/68211972?v=4)](https://github.com/lam0819 "lam0819 (3 commits)")[![ctf0](https://avatars.githubusercontent.com/u/7388088?v=4)](https://github.com/ctf0 "ctf0 (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)")[![nateritter](https://avatars.githubusercontent.com/u/198798?v=4)](https://github.com/nateritter "nateritter (2 commits)")[![ankurk91](https://avatars.githubusercontent.com/u/6111524?v=4)](https://github.com/ankurk91 "ankurk91 (2 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)")[![royduin](https://avatars.githubusercontent.com/u/1703233?v=4)](https://github.com/royduin "royduin (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

databasei18nlaravellaravel-packagetranslationtranslatemodeleloquentmultilinguallaravel-translatablei8nsolutionforest

### Embed Badge

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

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

###  Alternatives

[spatie/laravel-translatable

A trait to make an Eloquent model hold translations

2.5k26.4M508](/packages/spatie-laravel-translatable)[spatie/laravel-medialibrary

Associate files with Eloquent models

6.1k43.2M615](/packages/spatie-laravel-medialibrary)[mongodb/laravel-mongodb

A MongoDB based Eloquent model and Query builder for Laravel

7.1k8.4M91](/packages/mongodb-laravel-mongodb)[kirschbaum-development/eloquent-power-joins

The Laravel magic applied to joins.

1.6k32.6M45](/packages/kirschbaum-development-eloquent-power-joins)[psalm/plugin-laravel

Psalm plugin for Laravel

3355.3M341](/packages/psalm-plugin-laravel)[dyrynda/laravel-model-uuid

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

4843.1M9](/packages/dyrynda-laravel-model-uuid)

PHPackages © 2026

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