{"id":40488971,"url":"https://github.com/davesweb/laravel-translatable","last_synced_at":"2026-01-20T18:49:54.319Z","repository":{"id":56963224,"uuid":"419839809","full_name":"davesweb/laravel-translatable","owner":"davesweb","description":"Add translations to Eloquent models","archived":false,"fork":false,"pushed_at":"2021-11-03T19:56:55.000Z","size":78,"stargazers_count":0,"open_issues_count":0,"forks_count":0,"subscribers_count":1,"default_branch":"main","last_synced_at":"2025-05-24T00:50:25.357Z","etag":null,"topics":["eloquent","laravel","translations"],"latest_commit_sha":null,"homepage":"","language":"PHP","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/davesweb.png","metadata":{"files":{"readme":"readme.md","changelog":null,"contributing":null,"funding":null,"license":"license","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null}},"created_at":"2021-10-21T18:44:44.000Z","updated_at":"2021-11-04T13:00:06.000Z","dependencies_parsed_at":"2022-08-21T05:40:20.600Z","dependency_job_id":null,"html_url":"https://github.com/davesweb/laravel-translatable","commit_stats":null,"previous_names":[],"tags_count":1,"template":false,"template_full_name":null,"purl":"pkg:github/davesweb/laravel-translatable","repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/davesweb%2Flaravel-translatable","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/davesweb%2Flaravel-translatable/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/davesweb%2Flaravel-translatable/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/davesweb%2Flaravel-translatable/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/davesweb","download_url":"https://codeload.github.com/davesweb/laravel-translatable/tar.gz/refs/heads/main","sbom_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/davesweb%2Flaravel-translatable/sbom","scorecard":null,"host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":286080680,"owners_count":28609209,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2026-01-20T16:10:39.856Z","status":"ssl_error","status_checked_at":"2026-01-20T16:10:39.493Z","response_time":117,"last_error":"SSL_read: unexpected eof while reading","robots_txt_status":"success","robots_txt_updated_at":"2025-07-24T06:49:26.215Z","robots_txt_url":"https://github.com/robots.txt","online":false,"can_crawl_api":true,"host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["eloquent","laravel","translations"],"created_at":"2026-01-20T18:49:54.234Z","updated_at":"2026-01-20T18:49:54.304Z","avatar_url":"https://github.com/davesweb.png","language":"PHP","funding_links":["https://www.patreon.com/davesweb"],"categories":[],"sub_categories":[],"readme":"# Laravel Translatable\n\nAdd translation to Laravel Models by adding a translation model and a translation database table.\n\n## Installation\n\nVia composer\n\n```shell\ncomposer require davesweb/laravel-translatable\n```\n\n## Configuration\n\n- Add the `Davesweb\\LaravelTranslatable\\Traits\\HasTranslations` trait to the model you want to translate.\n- Create a translation model which holds the translatable attributes, but instead of extending the Laravel `Model`\n  class, extend the `Davesweb\\LaravelTranslatable\\Models\\TranslationModel` class.\n- Create a migration to create the database table for your translation model.\n  There are also commands available for generating these models and migrations for you.\n  \nAs long as the names of the translation model and the foreign key column follow the naming conventions, the\npackage will automatically find the correct models and set the correct relations.\n\nThe name of the translation model should be the same as the model that is being translated with the suffix\n`Translation`, for example `App\\Page` and `App\\PageTranslation`.\n\nThe name of the foreign key column should be the name of the model that is being translated in snake case,\nsuffixed with the name of the primary key. For instance `page_id`.\nIf your model names differ from te naming convention you can specify them yourself on the models. For the\n  model to be translated, add a `$translation` property.\n  \n```php\n\u003c?php\n\nnamespace App;\n\nuse Illuminate\\Database\\Eloquent\\Model;\n\nclass Page extends Model\n{\n    protected string $translation = App\\DifferentPageTranslationName::class;\n}\n```\n\nFor translation models you can set the `translates` property.\n\n```php\n\u003c?php\n\nnamespace App;\n\nuse Davesweb\\LaravelTranslatable\\Models\\TranslationModel;\n\nclass PageTranslation extends TranslationModel\n{\n    protected string $translates = App\\SomeOtherModel::class;\n}\n```\n\n## Docker\n\nThis package has a Docker setup for easy development. To use it, simply copy the `docker-compose.yaml.dist` file \nto `docker-compose.yaml` and adjust anything you'd want to adjust.\n\n```shell\ncp docker-compose.yaml.dist docker-compose.yaml\n```\n\nThen up the container:\n\n```shell\ndocker-compose up -d\n```\n\nNow you can log in to the container:\n\n```shell\ndocker-compose exec app bash\n```\n\n## Usage\n\nBecause the translations are just a one-to-many relation of the model you can use them the same way as any other \none-to-many relationship in Laravel. There are a few helper methods to make it easier to use the package, but the base \nis just a relation.\n\n### Fetching translations\n\nYou can get the collection of all translations from a model by calling `getTranslations()`.\n\n```php\n\u003c?php\n\n$page = App\\Page::query()-\u003efindOrFail(1);\n\n$translations = $page-\u003egetTranslations();\n```\n\nIn order to optimize your database calls you should eager-load your translations:\n\n```php\n\u003c?php\n\n$page = App\\Page::query()-\u003ewith('translations')-\u003efindOrFail(1);\n\n$translations = $page-\u003egetTranslations();\n```\n\nOr load only the translation in the locale you want:\n\n```php\n\u003c?php\n\nuse Illuminate\\Database\\Eloquent\\Builder;\n\n$locale = 'nl';\n\n$page = App\\Page::query()-\u003ewhereHas('translations', function(Builder $query) use ($locale) {\n    $query-\u003ewhere('locale', '=', $locale);\n})-\u003efindOrFail(1);\n\n$translations = $page-\u003egetTranslations();\n```\n\nYou can fetch a single translation by calling `getTranslation('locale')`.\n\n```php\n\u003c?php\n$page = App\\Page::query()-\u003ewith('translations')-\u003efindOrFail(1);\n\n$englishTranslation = $page-\u003egetTranslation('en');\n$dutchTranslation   = $page-\u003egetTranslation('nl');\n```\n\nTo get the translation in the current locale the app is in you can call `translation()`.\n\n```php\n\u003c?php\n\napp()-\u003esetLocale('en');\n\n$page = App\\Page::query()-\u003ewith('translations')-\u003efindOrFail(1);\n\n$englishTranslation = $page-\u003etranslation();\n```\n\nTo get the value of a translated attribute in the current locale the app is in you can call `translate('attribute')`.\n\n```php\n\u003c?php\n\napp()-\u003esetLocale('en');\n\n$page = App\\Page::query()-\u003ewith('translations')-\u003efindOrFail(1);\n\n$englishTitle = $page-\u003etranslate('title');\n```\n\nTo get the value of a translated attribute in a different locale then the current app locale is in you can call \n`translate('attribute', 'locale')` with the desired locale.\n\n```php\n\u003c?php\n\napp()-\u003esetLocale('en');\n\n$page = App\\Page::query()-\u003ewith('translations')-\u003efindOrFail(1);\n\n$dutchTitle = $page-\u003etranslate('title', 'nl');\n```\n\n### Saving translations\n\nSaving a translation is done just like any other Laravel Model. Set the attributes on the TranslationModel in your \npreferred way, then save it to the model by calling `$page-\u003etranslations()-\u003esave($pageTranslation);`\n\n## Artisan commands\n\nThere are three command available for this package:\n\n```shell\nphp artisan make:translatable {name}\n```\n\nThis command creates a new Model class with the given {name} and the `HasTranslations` trait already set.\nThis command has a few options as well:\n\n- `-t` or `--translation`: Adding this will also make a TranslationModel class for the model.\n- `-m` or `--migration`: Adding this will also make a migration for both the model and the TranslationModel\n- `-a` or `-all`: Use all available options.\n\n```shell\nphp artisan make:translation {name}\n```\n\nThis command creates a new TranslationModel class with the given {name}.\n\n```shell\nphp artisan make:translatable-migration {name}\n```\n\nThis command creates a new migration with both the model and translation model migrations for the given {name}. The \n{name} should be the classname of the model, for instance `App\\Page`.\n\n## Testing\n\nTo run the testsuite, simply run\n\n```shell\ndocker-compose exec app composer test\n```\n\n## Code style\n\nThis package uses PHP CS Fixer to enforce code style. To run it, run \n\n```shell\ndocker-compose exec app composer cs-fixer\n```\n\n## License\n\nThis package is licensed under the MIT license, which basically means you can do whatever your want with this package. \nHowever, if you found this package useful, please consider buying me a beer or subscribing to premium email support \nover on [Patreon](https://www.patreon.com/davesweb), it's really appreciated!","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fdavesweb%2Flaravel-translatable","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fdavesweb%2Flaravel-translatable","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fdavesweb%2Flaravel-translatable/lists"}