Download the PHP package esign/laravel-helpermodel-translatable without Composer
On this page you can find all versions of the php package esign/laravel-helpermodel-translatable. It is possible to download/install these versions without Composer. Possible dependencies are resolved automatically.
Download esign/laravel-helpermodel-translatable
More information about esign/laravel-helpermodel-translatable
Files in esign/laravel-helpermodel-translatable
Package laravel-helpermodel-translatable
Short Description A laravel package to make your eloquent models translatable.
License MIT
Homepage https://github.com/esign/laravel-helpermodel-translatable
Informations about the package laravel-helpermodel-translatable
Make Eloquent models translatable
This package allows you to make eloquent models translatable by using a seperate model for storing translations, e.g. Post
and PostTranslation
.
Installation
You can install the package via composer:
The package will automatically register a service provider.
Next up, you can publish the configuration file:
The config file will be published as config/helpermodel-translatable.php
with the following content:
Usage
Preparing your model
To make your model translatable you need to use the Esign\HelperModelTranslatable\HelperModelTranslatable trait on the model. Next up, you should define which fields are translatable by adding a public $translatable property.
Next up, you may create a helper model just like you're used to:
Retrieving translations
To retrieve a translation in the current locale you may use the attribute you have defined in the translatable
property. Or you could use the getTranslation
method:
To retrieve a translation in a specific locale you may use the fully suffixed attribute or pass the locale to the getTranslation
method:
To check if a translated attribute exists, you may use the hasTranslation
method:
In case you need to check if the actual translation model exists, you may use the hasTranslationModel
method:
To retrieve the actual translation model you may use the getTranslationModel
method:
In case you do not supply a locale, the current locale will be used.
Using a fallback
This package allows you to return the value of an attribute's fallback_locale
defined in the config/app.php
of your application.
The third useFallbackLocale
parameter of the getTranslation
method may be used to control this behaviour:
Or you may use dedicated methods for this:
You may configure the fallback locale by overwriting the getFallbackLocale
method from the HelperModelTranslatable
trait. The locale that was requested initially is passed as a parameter:
Customizing the relationship
By convention, this package assumes your helper model follows the same name of your main model suffixed by Translation
, e.g. Post
and PostTranslation
.
This model is used to load the translations
relationship that you may customize by either defining the model / foreign key or by overwriting the relationship alltogether.
In case you need to customize the default relationship name you may do so by overwriting the helperModelRelation
property on your model:
It's also possible to use a different relationship dynamically by using the useHelperModelRelation
method:
Scopes
This package also ships with a few scopes that allow you to set constraints for the translations relationship:
Testing
License
The MIT License (MIT). Please see License File for more information.
All versions of laravel-helpermodel-translatable with dependencies
illuminate/database Version ^7.0|^8.0|^9.0|^10.0|^11.0
illuminate/support Version ^7.0|^8.0|^9.0|^10.0|^11.0