spatie / laravel-translatable
A trait to make an Eloquent model hold translations
Fund package maintenance!
spatie
Installs: 13 837 246
Dependents: 356
Suggesters: 8
Security: 0
Stars: 2 242
Watchers: 26
Forks: 280
Open Issues: 6
Requires
- php: ^8.0
- illuminate/database: ^9.0|^10.0|^11.0
- illuminate/support: ^9.0|^10.0|^11.0
- spatie/laravel-package-tools: ^1.11
Requires (Dev)
- mockery/mockery: ^1.4
- orchestra/testbench: ^7.0|^8.0|^9.0
- pestphp/pest: ^1.20|^2.0
- dev-main
- 6.8.0
- 6.7.1
- 6.7.0
- 6.6.2
- 6.6.1
- 6.6.0
- 6.5.5
- 6.5.4
- 6.5.3
- 6.5.2
- 6.5.1
- 6.5.0
- 6.4.0
- 6.3.0
- 6.2.0
- 6.1.0
- 6.0.0
- v5.x-dev
- 5.2.0
- 5.1.0
- 5.0.3
- 5.0.2
- 5.0.1
- 5.0.0
- 4.6.0
- 4.5.2
- 4.5.1
- 4.5.0
- 4.4.3
- 4.4.2
- 4.4.1
- 4.4.0
- 4.3.2
- 4.3.1
- 4.3.0
- 4.2.2
- 4.2.1
- 4.2.0
- 4.1.4
- 4.1.3
- 4.1.2
- 4.1.1
- 4.1.0
- 4.0.0
- v3.x-dev
- 3.1.3
- 3.1.2
- 3.1.1
- 3.1.0
- 3.0.1
- 3.0.0
- 2.2.1
- 2.2.0
- 2.1.5
- 2.1.4
- 2.1.3
- 2.1.2
- 2.1.1
- 2.1.0
- 2.0.0
- 1.3.0
- 1.2.2
- 1.2.1
- 1.2.0
- 1.1.2
- 1.1.1
- 1.1.0
- 1.0.0
- 0.0.1
- dev-shift-57946
This package is auto-updated.
Last update: 2024-10-24 15:01:40 UTC
README
This package contains a trait HasTranslations
to make Eloquent models translatable. Translations are stored as json. There is no extra table needed to hold them.
use Illuminate\Database\Eloquent\Model; use Spatie\Translatable\HasTranslations; class NewsItem extends Model { use HasTranslations; // ... }
After the trait is applied on the model you can do these things:
$newsItem = new NewsItem; $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' // If you want to query records based on locales, you can use the `whereLocale` and `whereLocales` methods. NewsItem::whereLocale('name', 'en')->get(); // Returns all news items with a name in English NewsItem::whereLocales('name', ['en', 'nl'])->get(); // Returns all news items with a name in English or Dutch // Returns all news items that has name in English with value `Name in English` NewsItem::query()->whereJsonContainsLocale('name', 'en', 'Name in English')->get(); // Returns all news items that has name in English or Dutch with value `Name in English` NewsItem::query()->whereJsonContainsLocales('name', ['en', 'nl'], 'Name in English')->get(); // The last argument is the "operand" which you can tweak to achieve something like this: // Returns all news items that has name in English with value like `Name in...` NewsItem::query()->whereJsonContainsLocale('name', 'en', 'Name in%', 'like')->get(); // Returns all news items that has name in English or Dutch with value like `Name in...` NewsItem::query()->whereJsonContainsLocales('name', ['en', 'nl'], 'Name in%', 'like')->get();
Support us
We invest a lot of resources into creating best in class open source packages. You can support us by buying one of our paid products.
We highly appreciate you sending us a postcard from your hometown, mentioning which of our package(s) you are using. You'll find our address on our contact page. We publish all received postcards on our virtual postcard wall.
Documentation
All documentation is available on our documentation site.
Testing
composer test
Contributing
Please see CONTRIBUTING for details.
Security
If you've found a bug regarding security please mail security@spatie.be instead of using the issue tracker.
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, Kruikstraat 22, 2018 Antwerp, Belgium.
We publish all received postcards on our company website.
Credits
We got the idea to store translations as json in a column from Mohamed Said. Parts of the readme of his multilingual package were used in this readme.
License
The MIT License (MIT). Please see License File for more information.