PHPackages                             zai/laravel-eloquent-multilingualization - 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. zai/laravel-eloquent-multilingualization

ActiveLibrary

zai/laravel-eloquent-multilingualization
========================================

Add multilingual support to laravel eloquent models

v1.0.2(8y ago)124MITPHPPHP &gt;=5.4.0

Since Sep 4Pushed 8y ago1 watchersCompare

[ Source](https://github.com/zaichaopan/laravel-eloquent-multilingualization)[ Packagist](https://packagist.org/packages/zai/laravel-eloquent-multilingualization)[ RSS](/packages/zai-laravel-eloquent-multilingualization/feed)WikiDiscussions master Synced today

READMEChangelog (2)Dependencies (4)Versions (3)Used By (0)

laravel-eloquent-multilingualization
====================================

[](#laravel-eloquent-multilingualization)

Add multilingual support to your laravel eloquent models in a breeze

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

[](#installation)

### Step 1: Install package

[](#step-1-install-package)

Executing the following command to add the package in your composer.json

```
composer require zai/laravel-eloquent-multilingualization
```

For laravel 5.4, add the service provider to app/config/app.php

```
 Zai\Translate\TranslationServiceProvider::class,
```

For laravel 5.5, because of package auto-discovery, there is **no need** to add service provider to app/config/app.php

### Step 2: Migration

[](#step-2-migration)

Executing the following commands to add translations table. Only one table is needed for translating any Eloquent models

```
php artisan translations:table

php artisan migrate
```

Usage
-----

[](#usage)

### Use Translatable trait in the model you want to translate

[](#use-translatable-trait-in-the-model-you-want-to-translate)

Let's say the model is called Article. In the Article model

```
use Zai\Translate\Translatable;

use Illuminate\Database\Eloquent\Model;

class Article extends Model
{
    use Translatable;
}
```

### Define translatables property in the model

[](#define-translatables-property-in-the-model)

Take the same Article model as example. We want to translate title and body of an article

```
protected $translatables = [
    'title',
    'body'
];
```

### Add Translation

[](#add-translation)

Using method: **addTranslation**. It takes an **associative** array as parameter. Simply put the translation data in the array, including which **language** it is using key **locale**.

```
// create a new article
$article = Article::create([
    'title' => 'Hello',
    'body' => 'laravel is awesome!'
]);

// add translation to the article
$article->addTranslation([
    'locale' => 'fr',
    'title' => 'Bonjour',
    'body' => 'laravel est génial!'
]);
```

The method only adds keys that exist in the translatables property of the model. Any other keys will be **ignored**. If a key in the translatables property is missing in the parameter. It will still be **inserted**, but the value will become **empty string**.

If you submit the translation data in the form. In your ArticleTranslationsController, simply using

```
public function store(Article $article)
{
    $article->addTranslation(request()->all();
}
```

Remember to include **locale input filed** in your form, so it can be posted in the request. If **addTranslation** method is applied to an **existing** translation, it will **update** the existing translation with values in the parameter.

### Display translation

[](#display-translation)

Using **translation** attribute provided by the trait, e.g, **$article-&gt;translation-&gt;title**. It will return correct translation based on what the **current locale** is (the value returned by **App::getLocale()**)

For default locale or if the translation of a locale is missing, it will return the values in the model.

```
// create a new title
$article = Article::create([
    'title' => 'Hello',
    'body' => 'laravel is awesome!'
]);

// for default locale, or no translations existing
$article->translation->title; // the output is Hello
$article->translation->body;  // the out is laravel is awesome!

// after adding translation
$article->addTranslation([
    'locale' => 'fr',
    'title' => 'Bonjour',
    'body' => 'laravel est génial!'
]);

// set the locale to fr
App::setLocale('fr');

$article->translation->title; // the output is Bonjour
$article->translation->body;  // the out is laravel est génial!

// for a no existing locale
App::setLocale('zh');

$article->translation->title; // the output is Hello
$article->translation->body;  // the out is laravel is awesome!
```

### Update translation

[](#update-translation)

Using method: **updateTranslation**. It takes an **associative array** as parameter. Simply put the updated translation data in the array, including which **language** it is.

```
// add translation to the article
$article->updateTranslation([
    'locale' => 'fr',
    'title' => 'updated title',
    'body' => 'updated body content here'
]);
```

If you update the translation using form, in your ArticleTranslationsController

```
public function update(Article $article)
{
    $article->updateTranslation(request()->all());
}
```

If you update to a translation which **doesn't exist**, it will **insert** a new translation.

### Delete a translation

[](#delete-a-translation)

Using method **deleteTranslation**. It takes a string which specifies which language of the translation you want to delete as parameter.

```
$article->deleteTranslation('fr');
```

### Delete all translations

[](#delete-all-translations)

Using method **deleteTranslations** which takes no parmaters

```
$article->deleteTranslations();
```

### Check translations exist

[](#check-translations-exist)

Using method **hasTranslations** which takes no paramters. The method returns boolean to indicate if the record has translations or not.

```
 $article->hasTranslations();
```

### Check translation of a locale exists

[](#check-translation-of-a-locale-exists)

Using method **hasTranslation**. It takes a string as parameter which specifies the locale you are checking. The method returns boolean to indicate if the translation of a specific locale exists or not.

```
$article->hasTranslation();
```

### Prevent N+1 problem

[](#prevent-n1-problem)

To prevent N+1 problem, eager load translations in your model

```
class Article extends Model
{
    use Translatable;

    protected $with = ['translations'];

    protected $translatables = [
        'title',
        'body'
    ];
}
```

### Appending Values To JSON

[](#appending-values-to-json)

Add the attribute **translation** to the appends property on the model

```
class Article extends Model
{
    use Translatable;

    protected $with = ['translations'];

    protected $appends = ['translation'];

    protected $translatables = [
        'title',
        'body'
    ];
}
```

###  Health Score

26

—

LowBetter than 43% of packages

Maintenance20

Infrequent updates — may be unmaintained

Popularity8

Limited adoption so far

Community7

Small or concentrated contributor base

Maturity59

Maturing project, gaining track record

 Bus Factor1

Top contributor holds 100% 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 ~0 days

Total

2

Last Release

3171d ago

### Community

Maintainers

![](https://www.gravatar.com/avatar/5dfb2782ff22c81ce8095946983c3d437156ed09b132ec93727da68a14fa7db0?d=identicon)[zaichaopan](/maintainers/zaichaopan)

---

Top Contributors

[![zaichaopan](https://avatars.githubusercontent.com/u/16104572?v=4)](https://github.com/zaichaopan "zaichaopan (20 commits)")

###  Code Quality

TestsPHPUnit

### Embed Badge

![Health badge](/badges/zai-laravel-eloquent-multilingualization/health.svg)

```
[![Health](https://phpackages.com/badges/zai-laravel-eloquent-multilingualization/health.svg)](https://phpackages.com/packages/zai-laravel-eloquent-multilingualization)
```

###  Alternatives

[fumeapp/modeltyper

Generate TypeScript interfaces from Laravel Models

196277.9k](/packages/fumeapp-modeltyper)[aedart/athenaeum

Athenaeum is a mono repository; a collection of various PHP packages

255.2k](/packages/aedart-athenaeum)

PHPackages © 2026

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