Uh oh!
There was an error while loading.Please reload this page.
- Notifications
You must be signed in to change notification settings - Fork295
Making Eloquent models translatable
License
spatie/laravel-translatable
Folders and files
| Name | Name | Last commit message | Last commit date | |
|---|---|---|---|---|
Repository files navigation
This package contains a traitHasTranslations to make Eloquent models translatable. Translations are stored as json. There is no extra table needed to hold them.
useIlluminate\Database\Eloquent\Model;useSpatie\Translatable\HasTranslations;class NewsItemextends Model{use HasTranslations;public$translatable = ['name'];// translatable attributes// ...}
After the trait is applied on the model you can do these things:
$newsItem =newNewsItem;$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'$newsItem->getTranslations('name');// returns an array of all name translations// You can translate nested keys of a JSON column using the -> notation// First, add the path to the $translatable array, e.g., 'meta->description'$newsItem ->setTranslation('meta->description','en','Description in English') ->setTranslation('meta->description','nl','Beschrijving in het Nederlands') ->save();$attributeKey ='meta->description';$newsItem->$attributeKey;// Returns 'Description in English'$newsItem->getTranslation('meta->description','nl');// Returns 'Beschrijving in het Nederlands'
Also providing scoped queries for retrieving records based on locales
// Returns all news items with a name in EnglishNewsItem::whereLocale('name','en')->get();// Returns all news items with a name in English or DutchNewsItem::whereLocales('name', ['en','nl'])->get();// 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();
We invest a lot of resources into creatingbest in class open source packages. You can support us bybuying 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 onour contact page. We publish all received postcards onour virtual postcard wall.
All documentation is availableon our documentation site.
composertestPlease seeCONTRIBUTING for details.
If you've found a bug regarding security please mailsecurity@spatie.be instead of using the issue tracker.
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 postcardson our company website.
We got the idea to store translations as json in a column fromMohamed Said. Parts of the readme ofhis multilingual package were used in this readme.
The MIT License (MIT). Please seeLicense File for more information.
About
Making Eloquent models translatable
Topics
Resources
License
Contributing
Uh oh!
There was an error while loading.Please reload this page.
Stars
Watchers
Forks
Sponsor this project
Uh oh!
There was an error while loading.Please reload this page.
Uh oh!
There was an error while loading.Please reload this page.

