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

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

ahmedash95/laravel-translatable
===============================

A trait to make an Eloquent model hold translations

1.1.4(9y ago)068MITPHPPHP &gt;=5.6

Since Apr 8Pushed 9y ago1 watchersCompare

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

READMEChangelog (2)Dependencies (4)Versions (8)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/88fbac3e49dc28537d5604646961f5e1d2a6578a1cd2249d89f6b499bdca1972/68747470733a2f2f696d672e736869656c64732e696f2f7061636b61676973742f762f61686d656461736839352f6c61726176656c2d7472616e736c617461626c652e7376673f7374796c653d666c61742d737175617265)](https://packagist.org/packages/ahmedash95/laravel-translatable)[![Software License](https://camo.githubusercontent.com/55c0218c8f8009f06ad4ddae837ddd05301481fcf0dff8e0ed9dadda8780713e/68747470733a2f2f696d672e736869656c64732e696f2f62616467652f6c6963656e73652d4d49542d627269676874677265656e2e7376673f7374796c653d666c61742d737175617265)](LICENSE.md)[![Build Status](https://camo.githubusercontent.com/d4bd157b5a16e12b67d84b7762100051d09018704ef502e98bd7b5ca0e30d65a/68747470733a2f2f696d672e736869656c64732e696f2f7472617669732f61686d656461736839352f6c61726176656c2d7472616e736c617461626c652f6d61737465722e7376673f7374796c653d666c61742d737175617265)](https://travis-ci.org/ahmedash95/laravel-translatable)[![SensioLabsInsight](https://camo.githubusercontent.com/4859a64cc90c88d3b0fc472a3aadd21c7a7bc5cc191dd11358bb46c845c42efb/68747470733a2f2f696d672e736869656c64732e696f2f73656e73696f6c6162732f692f63343737383030352d326235662d346364372d623462322d3962313264333236646465642e7376673f7374796c653d666c61742d737175617265)](https://insight.sensiolabs.com/projects/c4778005-2b5f-4cd7-b4b2-9b12d326dded)[![Quality Score](https://camo.githubusercontent.com/ac8997d0be572201e82841ee1cf0894393a6bad60e516a6579db9ae0af01a35f/68747470733a2f2f696d672e736869656c64732e696f2f7363727574696e697a65722f672f61686d656461736839352f6c61726176656c2d7472616e736c617461626c652e7376673f7374796c653d666c61742d737175617265)](https://scrutinizer-ci.com/g/ahmedash95/laravel-translatable)[![StyleCI](https://camo.githubusercontent.com/7c96715a72da82508feeeb6f9c3a8b1de01ee21c705858d5b20fd97241731a02/68747470733a2f2f7374796c6563692e696f2f7265706f732f35353639303434372f736869656c643f6272616e63683d6d6173746572)](https://styleci.io/repos/55690447)[![Total Downloads](https://camo.githubusercontent.com/9b3744d4401deedd75698e2a4b35f424ec770e18a68c78c51dd531f5bb1a64bb/68747470733a2f2f696d672e736869656c64732e696f2f7061636b61676973742f64742f61686d656461736839352f6c61726176656c2d7472616e736c617461626c652e7376673f7374796c653d666c61742d737175617265)](https://packagist.org/packages/ahmedash95/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 you are required to send us a postcard from your hometown, mentioning which of our package(s) you are using.

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

The best postcards will get published on the open source page on our website.

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

[](#installation)

You can install the package via composer:

```
composer require ahmedash95/laravel-translatable
```

Next up, the service provider must be registered:

```
// config/app.php
'providers' => [
    ...
    Spatie\Translatable\TranslatableServiceProvider::class,

];
```

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

#### Forgetting a translation

[](#forgetting-a-translation)

```
public function forgetTranslation(string $attributeName, 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::whereRaw('name->"$.en" = \'Name in English\'')->get();
```

In laravel 5.2.23 and above you can use the fluent syntax:

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

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

28

—

LowBetter than 52% of packages

Maintenance20

Infrequent updates — may be unmaintained

Popularity8

Limited adoption so far

Community13

Small or concentrated contributor base

Maturity63

Established project with proven stability

 Bus Factor1

Top contributor holds 70.4% 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 ~41 days

Recently: every ~56 days

Total

7

Last Release

3485d ago

Major Versions

0.0.1 → 1.0.02016-04-11

PHP version history (3 changes)0.0.1PHP ^7.0

1.1.3PHP ^5.6

1.1.4PHP &gt;=5.6

### Community

Maintainers

![](https://www.gravatar.com/avatar/a8f7720b07ee7addde37df48b85d7de8e127f043d7c1b7aacd0af79328301f0d?d=identicon)[ahmedash95](/maintainers/ahmedash95)

---

Top Contributors

[![freekmurze](https://avatars.githubusercontent.com/u/483853?v=4)](https://github.com/freekmurze "freekmurze (57 commits)")[![ahmedash95](https://avatars.githubusercontent.com/u/8272048?v=4)](https://github.com/ahmedash95 "ahmedash95 (14 commits)")[![sebastiandedeyne](https://avatars.githubusercontent.com/u/1561079?v=4)](https://github.com/sebastiandedeyne "sebastiandedeyne (7 commits)")[![drbyte](https://avatars.githubusercontent.com/u/404472?v=4)](https://github.com/drbyte "drbyte (1 commits)")[![jorenvanhee](https://avatars.githubusercontent.com/u/231202?v=4)](https://github.com/jorenvanhee "jorenvanhee (1 commits)")[![Propaganistas](https://avatars.githubusercontent.com/u/6680176?v=4)](https://github.com/Propaganistas "Propaganistas (1 commits)")

---

Tags

spatietranslatemodeleloquentmultilinguallaravel-translatablei8n

###  Code Quality

TestsPHPUnit

### Embed Badge

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

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

###  Alternatives

[spatie/laravel-translatable

A trait to make an Eloquent model hold translations

2.4k25.2M498](/packages/spatie-laravel-translatable)[spatie/laravel-translation-loader

Store your language lines in the database, yaml or other sources

8453.1M62](/packages/spatie-laravel-translation-loader)[tomatophp/filament-translations

Manage your translation with DB and cache, you can scan your languages tags like trans(), \_\_(), and get the string inside and translate them use UI.

6333.2k3](/packages/tomatophp-filament-translations)

PHPackages © 2026

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