Astrotomic / laravel-translatable

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

translation field in json return #246

Closed abdooobsl closed 2 years ago

abdooobsl commented 2 years ago

hi how to get the translated value only when i get all records like this

$packages = Package::all(); it returns like this

{
      "id": 1,
      "price": 50,
      "currency": "EGP",
      "status": 1,
      "period": 1,
      "created_at": "2021-06-16T10:22:34.000000Z",
      "updated_at": "2021-06-16T10:46:40.000000Z",
      "name": "economic",
      "description": "Number of branches 1, number of windows 2, number of users 2, number of guests 1000 per month الضيوف",
      "translations": [
        {
          "id": 2,
          "package_id": 1,
          "locale": "ar",
          "name": "اقتصادي",
          "description": "عدد الفروع 1,عدد الشبابيك 2,عدد المستخدمين 2,عدد الضيوف 1000 شهريا"
        },
        {
          "id": 1,
          "package_id": 1,
          "locale": "en",
          "name": "economic",
          "description": "Number of branches 1, number of windows 2, number of users 2, number of guests 1000 per month الضيوف"
        }
      ]
    },

name an description are loaded correct by lang but i want to hide translations i tried to set 'to_array_always_loads_translations' => true to false but name an description are not loaded at all

Gummibeer commented 2 years ago

Hey, you should use an Eloquent API Resource instead of directly returning the model collection.

https://github.com/Astrotomic/laravel-translatable/issues?q=resource https://laravel.com/docs/8.x/eloquent-resources