Astrotomic / laravel-translatable

A Laravel package for multilingual models
https://docs.astrotomic.info/laravel-translatable/
MIT License
1.23k stars 156 forks source link

Always load translations by locale key #241

Closed olivermbs closed 2 years ago

olivermbs commented 3 years ago

Currently when eager loading the translations relationship on a model, the output is as follows:

 $airport = Airport::with('translations')->findOrFail($id);

[{
    "id": 1,
    "airport_id": 219,
    "locale": "de",
    "name": "Flughafen"
}, {
    "id": 2,
    "airport_id": 219,
    "locale": "it",
    "name": "Aeroporto"
}, {
    "id": 3,
    "airport_id": 219,
    "locale": "en",
    "name": "Airport"
}]

The format I would like to use is the one which is produced by getTranslationsArray(), which comes out like this:

$airport['translationsArray'] = $airport['translations']->attributesToArray();

{
    "de": {
        "name": "Flughafen"
    },
    "it": {
        "name": "Aeroporto"
    },
    "en": {
        "name": "Airport"
    }
}

As I'm using InertiaJS/Vue, the second format is a lot easier to use in the templates. Is there any way to revert to this by default, without having to manipulate the eloquent result each time?

Thanks!

ordago commented 3 years ago
$category->getTranslationsArray()

[
    'ca' => [
        'name' => 'Anglès',
    ],
    'en' => [
        'name' => 'English',
    ],
    'es' => [
        'name' => 'Inglés',
    ],
];

https://docs.astrotomic.info/laravel-translatable/package/methods#gettranslationsarray

I'm also using Inertia&Vue

olivermbs commented 3 years ago
->getTranslationsArray()

Thank you! Do you send the translation array as a separate prop?

ordago commented 3 years ago

Do you send the translation array as a separate prop?

I'm including it with the other model values under the translations key to follow this:

https://docs.astrotomic.info/laravel-translatable/package/validation-rule-factory

github-actions[bot] commented 2 years ago

This issue is stale because it has been open 21 days with no activity. Remove stale label or comment or this will be closed in 7 days