spatie/laravel-translatable
Add multilingual fields to Eloquent models using a simple HasTranslations trait. Store translations as JSON on the model (no extra tables). Set/get translations per locale, switch app locale, fetch all translations, and even translate nested JSON keys via -> notation.
composer require spatie/laravel-translatableHasTranslations trait to any Eloquent model and define a $translatable array listing the attributes that should support translationsjson database type (or text as fallback); migration example: $table->json('name')$model->name = 'Hello'; or $model->setTranslation('name', 'fr', 'Bonjour');setTranslation() for explicit control over locale assignment; use magic property access ($model->title) when working with the current app locale$translatable (e.g., 'meta->description') for structured datawhereLocale(), whereLocales(), whereJsonContainsLocale()) to filter models by locale or value—crucial for SEO and multilingual admin interfacesforgetTranslation() and forgetAllTranslations() to handle partial or full locale removal cleanly, especially during content cleanup workflowstranslations() helper to generate seeded translations efficiently:
'title' => $this->translations(['en', 'nl'], ['English', 'Dutch'])
Translatable::fallback() to avoid silent UI gaps$translatable dynamically; it’s cached internally—define it as a static array propertywhereJsonContainsLocale(), ensure your DB supports JSON (MySQL ≥5.7 / PostgreSQL / MariaDB ≥10.2.3); otherwise, queries may fail or be slowTranslationHasBeenSetEvent fires on every setTranslation() call—useful for logging or syncing to external services, but be cautious of N+1 behavior in bulk operationsnews.{$id}.{$locale}How can I help you explore Laravel packages today?