spatie/laravel-translatable
Adds HasTranslations to Eloquent models to store translations in JSON columns—no extra tables. Define translatable attributes via PHP 8 attribute or $translatable property, then set/get per-locale values while model accessors return the current app locale.
You can install the package via composer:
composer require spatie/laravel-translatable
The required steps to make a model translatable are:
Spatie\Translatable\HasTranslations-trait.#[Translatable] PHP attribute.json-datatype in your database. If your database doesn't support json-columns, use text.Here's an example of a prepared model:
use Illuminate\Database\Eloquent\Model;
use Spatie\Translatable\Attributes\Translatable;
use Spatie\Translatable\HasTranslations;
#[Translatable('name', 'description')]
class NewsItem extends Model
{
use HasTranslations;
}
The attribute accepts a variadic list of column names, so you can pass as many as you need.
Alternatively, you can declare the translatable attributes via a public $translatable property:
use Illuminate\Database\Eloquent\Model;
use Spatie\Translatable\HasTranslations;
class NewsItem extends Model
{
use HasTranslations;
public array $translatable = ['name'];
}
When both the property and the attribute are present, their values are merged and deduplicated.
How can I help you explore Laravel packages today?