Download the PHP package slivka-b/laravel-translatable without Composer
On this page you can find all versions of the php package slivka-b/laravel-translatable. It is possible to download/install these versions without Composer. Possible dependencies are resolved automatically.
Download slivka-b/laravel-translatable
More information about slivka-b/laravel-translatable
Files in slivka-b/laravel-translatable
Package laravel-translatable
Short Description [fork] The package provides possibility to translate your Eloquent models into different languages using a single database table.
License MIT
Homepage https://github.com/nevadskiy/laravel-translatable
Informations about the package laravel-translatable
Laravel Translatable
This package is a fork of nevadskiy/laravel-translatable.
The package provides possibility to translate your Eloquent models into different languages using a single database table.
π¬ Features
- Auto-resolving model translations for the current locale.
- No need to rewrite existing migrations, models or views.
- Store all translations in the single 'translations' table.
- Works with model accessors & mutators & casts, even with JSON.
- Works with route model binding.
- Archive translations to improve searching experience.
- Provides useful events.
βοΈ Demo
β Requirements
- Laravel
9.0
or newer - PHP
8.0
or newer
π Installation
-
Install the package via composer.
- Optional. If you are not going to use translations for models with UUID primary keys, make the following:
-
Publish package migration
- Replace the line
$table->uuidMorphs('translatable');
with$table->morphs('translatable');
in the published migration.
- Run the migration command.
π¨ Making models translatable
-
Add the
HasTranslations
trait to your models which you want to make translatable. - Add the
$translatable
array to your models with attributes you want to be translatable.
Final model may look like this
π Documentation
Default locale values are stored in the original table as usual.
Values in non-default locales of each translatable model are stored in the single translations
table.
The package takes the default locale from the config('app.fallback_locale')
value.
Automatically store and retrieve translations of the model using translatable attributes
Manually store and retrieve translations of the model
Methods for reading translation
Method | Description |
---|---|
getTranslationOrDefault |
Retrieves a translation for the given attribute or a default value if a translation is missing. |
getTranslation |
Retrieves a translation for the given attribute or null if a translation is missing. |
getRawTranslation |
Retrieves a translation without any Eloquent accessors applied for the given attribute or null if a translation is missing. |
getDefaultTranslation |
Retrieves the value in a default locale. |
Translatable models creation
Note that translatable models will always be created in default locale even when the current locale is different. Any translations can be attached only to existing models.
Displaying collection of models
The package automatically eager loads translations of the current locale for you, so you can easily retrieve collection of models as usual.
Translations work with model accessors
Translations work with model mutators as well
Removing unused translations
The package automatically remove translations of deleted models respecting softDeletes, but if translatable models have been removed using query builder, their translations will exist in the database.
To manually remove all unused translations, run the php artisan translatable:remove-unused
command.
Querying models without translations
Sometimes you may need to query translatable model without the translations
relation. You can do this using withoutTranslations
scope.
Querying translations
You can execute queries on translatable models by translatable attributes.
It will also work with values in default locale.
If you want to query rows only by a specific locale, you should pass it by yourself.
$books = Book::whereTranslatable('title', 'ΠΠ½ΠΈΠ³Π° ΠΎ ΠΆΠΈΡΠ°ΡΠ°Ρ ', 'ru')->get();
Otherwise, the query builder will return matched rows within all available locales.
Also, you can use different operators for querying translations.
Ordering translations
Ordering models by a translatable attribute in the current locale.
Ordering models by a translatable attribute in the specific locale.
For more complex queries - feel free to use Laravel relation queries.
Disable auto loading
If you do not want to automatically load or save translations when you interact with a translatable property, you can disable the feature.
To disable it for a specific model, override the autoLoadTranslations
or autoSaveTranslations
methods in your model like so.
Or globally for every model.
Archiving translations
Sometimes it can be useful to archive some translations that will not be resolved automatically, but can be used for searching functionality. For example, you may store archived translation manually using the following code:
Now Old title
is associated with a post that allows to find the post using whereTranslatable
scope:
You can also pass null
as a third argument to the archiveTranslation
method when a locale is unknown.
If you do not pass null
, the current locale will be used instead.
Route model binding
Translatable model can be easily resolved using Route Model Binding feature.
All you need to do to let Laravel resolve models by a translatable attribute is to set the needed locale that you want to be used for querying models before a request will reach Illuminate\Routing\Middleware\SubstituteBindings::class
middleware.
The simplest solution is to create a new middleware, for example SetLocaleMiddleware
, attach it to the route where you want to resolve translatable models, and register the middleware in the $middlewarePriority
array of the app/Http/Kernel.php
file above the \Illuminate\Routing\Middleware\SubstituteBindings::class
class.
It may look like this:
More about sorting middleware here.
Using morph map
It is recommended to use morph map
for all translatable models to minimize coupling between a database and application structure.
More about morph maps here.
π Changelog
Please see CHANGELOG for more information what has changed recently.
β Contributing
Please see CONTRIBUTING for more information.
π Security
If you discover any security related issues, please e-mail me instead of using the issue tracker.
π License
The MIT License (MIT). Please see LICENSE for more information.