Download the PHP package davesweb/laravel-translatable without Composer
On this page you can find all versions of the php package davesweb/laravel-translatable. It is possible to download/install these versions without Composer. Possible dependencies are resolved automatically.
Download davesweb/laravel-translatable
More information about davesweb/laravel-translatable
Files in davesweb/laravel-translatable
Package laravel-translatable
Short Description Add translations to Eloquent models
License MIT
Informations about the package laravel-translatable
Laravel Translatable
Add translation to Laravel Models by adding a translation model and a translation database table.
Installation
Via composer
Configuration
- Add the
Davesweb\LaravelTranslatable\Traits\HasTranslations
trait to the model you want to translate. - Create a translation model which holds the translatable attributes, but instead of extending the Laravel
Model
class, extend theDavesweb\LaravelTranslatable\Models\TranslationModel
class. - Create a migration to create the database table for your translation model. There are also commands available for generating these models and migrations for you.
As long as the names of the translation model and the foreign key column follow the naming conventions, the package will automatically find the correct models and set the correct relations.
The name of the translation model should be the same as the model that is being translated with the suffix
Translation
, for example App\Page
and App\PageTranslation
.
The name of the foreign key column should be the name of the model that is being translated in snake case,
suffixed with the name of the primary key. For instance page_id
.
If your model names differ from te naming convention you can specify them yourself on the models. For the
model to be translated, add a $translation
property.
For translation models you can set the translates
property.
Docker
This package has a Docker setup for easy development. To use it, simply copy the docker-compose.yaml.dist
file
to docker-compose.yaml
and adjust anything you'd want to adjust.
Then up the container:
Now you can log in to the container:
Usage
Because the translations are just a one-to-many relation of the model you can use them the same way as any other one-to-many relationship in Laravel. There are a few helper methods to make it easier to use the package, but the base is just a relation.
Fetching translations
You can get the collection of all translations from a model by calling getTranslations()
.
In order to optimize your database calls you should eager-load your translations:
Or load only the translation in the locale you want:
You can fetch a single translation by calling getTranslation('locale')
.
To get the translation in the current locale the app is in you can call translation()
.
To get the value of a translated attribute in the current locale the app is in you can call translate('attribute')
.
To get the value of a translated attribute in a different locale then the current app locale is in you can call
translate('attribute', 'locale')
with the desired locale.
Saving translations
Saving a translation is done just like any other Laravel Model. Set the attributes on the TranslationModel in your
preferred way, then save it to the model by calling $page->translations()->save($pageTranslation);
Artisan commands
There are three command available for this package:
This command creates a new Model class with the given {name} and the HasTranslations
trait already set.
This command has a few options as well:
-t
or--translation
: Adding this will also make a TranslationModel class for the model.-m
or--migration
: Adding this will also make a migration for both the model and the TranslationModel-a
or-all
: Use all available options.
This command creates a new TranslationModel class with the given {name}.
This command creates a new migration with both the model and translation model migrations for the given {name}. The
{name} should be the classname of the model, for instance App\Page
.
Testing
To run the testsuite, simply run
Code style
This package uses PHP CS Fixer to enforce code style. To run it, run
License
This package is licensed under the MIT license, which basically means you can do whatever your want with this package. However, if you found this package useful, please consider buying me a beer or subscribing to premium email support over on Patreon, it's really appreciated!
All versions of laravel-translatable with dependencies
illuminate/support Version ^8.0
illuminate/console Version ^8.0
illuminate/database Version ^8.0
illuminate/filesystem Version ^8.0