Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Making Eloquent models translatable

License

NotificationsYou must be signed in to change notification settings

spatie/laravel-translatable

Repository files navigation

Logo for laravel-translatable

A trait to make Eloquent models translatable

Latest Version on PackagistMIT LicensedGitHub Workflow StatusTotal Downloads

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();

Support us

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.

Documentation

All documentation is availableon our documentation site.

Testing

composertest

Contributing

Please seeCONTRIBUTING for details.

Security

If you've found a bug regarding security please mailsecurity@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 postcardson our company website.

Credits

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.

Alternatives

License

The MIT License (MIT). Please seeLicense File for more information.

Sponsor this project

 

Languages


[8]ページ先頭

©2009-2025 Movatter.jp