- How do I make an Eloquent model translatable without creating a separate table?
- Use the `HasTranslations` trait and mark fields with `#[Translatable]` attributes or the `$translatable` property. Translations are stored as JSON in a single column, so no extra tables are needed. For example, `#[Translatable('name', 'description')]` on a model automatically handles multilingual storage.
- Does spatie/laravel-translatable work with Laravel 10+?
- Yes, this package supports Laravel 9+ and requires PHP 8.1+. It’s fully compatible with the latest Laravel versions, including 10, and follows modern Eloquent conventions. Check the [GitHub repo](https://github.com/spatie/laravel-translatable) for version-specific updates.
- Can I query models by translation content (e.g., find posts where 'name' equals 'Hello' in English)?
- Yes, the package provides `whereLocale` and `whereJsonContainsLocale` methods for querying translations directly in the database. For example, `Model::whereLocale('name', 'en', 'Hello')->get()` filters records where the English translation of `name` matches 'Hello'.
- What happens if a translation is missing for the current locale? How do I set a fallback?
- By default, the model returns `null` for missing translations. You can configure fallback logic globally via `Translatable::fallback()` or per-model using the `fallbackLocale` property. For example, `Translatable::fallback('en')` ensures English translations are used if a locale is missing.
- Is there a performance impact when using JSON columns for translations?
- JSON storage is efficient for simple lookups but may slow down complex queries (e.g., full-text search or aggregations) on older MySQL/MariaDB versions (pre-5.7/10.2.3). For production, ensure your database supports JSON functions or use application-level filtering. Indexing the JSON column can also improve performance.
- Can I translate nested JSON fields (e.g., meta.description) with this package?
- Yes, the package supports nested JSON keys. For example, you can translate `meta.description` by setting `setTranslation('meta->description', 'en', 'Value')`. Access nested translations with `getTranslation('meta->description', 'en')`. This avoids schema changes for complex data structures.
- How do I test models with translations in Laravel’s testing tools?
- Use `setTranslation` in your tests to mock translations. For example, `$model->setTranslation('name', 'en', 'Test Name')` before assertions. The package integrates seamlessly with Laravel’s testing helpers, including factories and `create()`. Test queries like `whereLocale` with `assertDatabaseHas`.
- What are the alternatives to spatie/laravel-translatable for multilingual Eloquent models?
- Alternatives include `spatie/laravel-model-translations` (separate tables), `knuckleswtf/laravel-translatable` (similar JSON approach), or custom solutions with polymorphic relations. This package stands out for its simplicity (no extra tables) and tight Laravel integration, but choose based on your need for query flexibility or scalability.
- How do I migrate an existing project using separate translation tables to this package?
- Refactor by adding a JSON column (e.g., `translations`) to your model table, then write a migration to extract existing translations into JSON format. Use `setTranslation` to repopulate data. This requires careful planning, especially for large datasets, but avoids schema changes for new projects.
- Does this package support PostgreSQL, or is it MySQL-only?
- While primarily optimized for MySQL 5.7+/MariaDB 10.2.3+, PostgreSQL support is possible with minor adjustments (e.g., custom queries for JSON functions). The package leverages Laravel’s database abstraction, so basic CRUD works across databases, but test JSON queries thoroughly in your target environment.