latevaweb / laravel-translatable
Laravel package to make Eloquent models attributes translatables
Installs: 1 103
Dependents: 0
Suggesters: 0
Security: 0
Stars: 0
Watchers: 1
Forks: 2
Open Issues: 4
Type:laravel-package
Requires
- php: ^7.3|^8.2|^8.3
Requires (Dev)
- orchestra/testbench: ^4.0|^5.0
- phpunit/phpunit: ^7.0|^8.0|^9.0
- dev-master
- 0.1.9
- 0.1.8
- 0.1.7
- 0.1.6
- 0.1.5
- 0.1.4
- 0.1.3
- 0.1.2
- 0.1.1
- 0.1.0
- dev-develop
- dev-dependabot/composer/symfony/http-kernel-4.4.50
- dev-dependabot/composer/league/flysystem-1.1.4
- dev-dependabot/composer/laravel/framework-6.20.26
- dev-dependabot/composer/symfony/http-foundation-4.4.13
- dev-analysis-wjjmmK
- dev-analysis-ajjKwx
- dev-analysis-nNN1e1
This package is auto-updated.
Last update: 2025-03-05 12:01:58 UTC
README
Make Eloquent model attributes translatables using Translations table
This package contains a trait to make Eloquent attributes translatable. Translations are stored in Translations database table.
Once the trait is installed on the model you can do these things:
$customer = new Customer; // An Eloquent model $customer ->setTranslation('greeting', 'en', 'Hello') ->setTranslation('greeting', 'es', 'Hola') ->save(); $customer->greeting; // Returns 'Hello' given that the current app locale is 'en' $customer->getTranslation('greeting', 'es'); // returns 'Hola' app()->setLocale('es'); $customer->greeting; // Returns 'Hola'
Installation
You can install the package via composer:
composer require latevaweb/laravel-translatable
If you want to change the default model or the default tables names, you could publish the config file:
php artisan vendor:publish --provider="LaTevaWeb\Translatable\TranslatableServiceProvider" --tag=config --force
You must publish the migration file to create polymorphic and main translations tables:
php artisan vendor:publish --provider="LaTevaWeb\Translatable\TranslatableServiceProvider" --tag=migrations --force
Making a model translatable
The required steps to make a model translatable are:
- First, you need to add the
LaTevaWeb\Translatable\Traits\Translatable
-trait. - Next, you should create a public static property
$translatable
which holds an array with all the names of attributes you wish to make translatable. - You have to create a field in the migration of your model type
string
andnullable
.
Here's an example of a prepared model:
use Illuminate\Database\Eloquent\Model; use LaTevaWeb\Translatable\Traits\Translatable; class NewsItem extends Model { use Translatable; protected $fillable = ['greeting']; public static $translatable = ['greeting']; }