Astrotomic / laravel-translatable

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

Filter collection based on specific locale #279

Closed solingenn closed 2 years ago

solingenn commented 2 years ago

`Collection is returning all translations and I don't know how to filter it using **laravel-translatable**. I have tried usingtranslation('en'), translatedIn('en')` methods and they don't work.

This is JSON repsonse of collection:

{
    "meta": {
        "current_page": 1,
        "data": [
            {
                "id": 1,
                "category_id": null,
                "status": "created",
                "created_at": "2022-04-23T19:52:55.000000Z",
                "updated_at": "2022-04-23T19:52:55.000000Z",
                "deleted_at": null,
                "meal_name": "Omelette",
                "translations": [
                    {
                        "id": 4,
                        "meal_id": 1,
                        "locale": "de",
                        "meal_name": "Omelette",
                        "slug": "omelette",
                        "description": "Gefalteter Pfannkuchen aus geschlagenen Eiern, manchmal mit der Zugabe von Gemüse, Käse oder Fleisch."
                    },
                    {
                        "id": 1,
                        "meal_id": 1,
                        "locale": "en",
                        "meal_name": "Omelette",
                        "slug": "omelette",
                        "description": "Folded pancake made of beaten eggs, sometimes with the addition of vegetables, cheese, or meat."
                    },
                    {
                        "id": 2,
                        "meal_id": 1,
                        "locale": "fr",
                        "meal_name": "Omelette",
                        "slug": "omelette",
                        "description": "Crêpe pliée à base d'œufs battus, parfois avec l'ajout de légumes, de fromage ou de viande."
                    },
                    {
                        "id": 3,
                        "meal_id": 1,
                        "locale": "it",
                        "meal_name": "Frittata",
                        "slug": "frittata",
                        "description": "Frittella ripiegata a base di uova sbattute, a volte con l'aggiunta di verdure, formaggio o carne."
                    }
                ]
            },
            {
                "id": 4,
                "category_id": 2,
                "status": "created",
                "created_at": "2022-04-23T19:52:57.000000Z",
                "updated_at": "2022-04-23T19:53:22.000000Z",
                "deleted_at": null,
                "meal_name": "Pasta bolognese",
                "translations": [
                    {
                        "id": 16,
                        "meal_id": 4,
                        "locale": "de",
                        "meal_name": "Bolognese-Nudeln",
                        "slug": "bolognese-nudeln",
                        "description": "Spaghetti Bolognese besteht aus Spaghetti mit einem italienischen Ragù (Fleischsoße) aus Hackfleisch und Tomaten."
                    },
                    {
                        "id": 13,
                        "meal_id": 4,
                        "locale": "en",
                        "meal_name": "Pasta bolognese",
                        "slug": "pasta-bolognese",
                        "description": "Spaghetti bolognese consists of spaghetti with an Italian ragù (meat sauce) made with minced beef and tomatoes."
                    },
                    {
                        "id": 14,
                        "meal_id": 4,
                        "locale": "fr",
                        "meal_name": "Pâtes bolognaises",
                        "slug": "pates-bolognaises",
                        "description": "Les spaghettis à la bolognaise se composent de spaghettis avec un ragù italien (sauce à la viande) à base de bœuf haché et de tomates."
                    },
                    {
                        "id": 15,
                        "meal_id": 4,
                        "locale": "it",
                        "meal_name": "Pasta al ragù",
                        "slug": "pasta-al-ragu",
                        "description": "Gli spaghetti alla bolognese sono spaghetti al ragù italiano (sugo di carne) a base di carne macinata e pomodori."
                    }
                ]
            }
        ],
        "first_page_url": "http://127.0.0.1:8000/api/jela-svijeta?page=1",
        "from": 1,
        "last_page": 1,
        "last_page_url": "http://127.0.0.1:8000/api/jela-svijeta?page=1",
        "links": [
            {
                "url": null,
                "label": "« Previous",
                "active": false
            },
            {
                "url": "http://127.0.0.1:8000/api/jela-svijeta?page=1",
                "label": "1",
                "active": true
            },
            {
                "url": null,
                "label": "Next »",
                "active": false
            }
        ],
        "next_page_url": null,
        "path": "http://127.0.0.1:8000/api/jela-svijeta",
        "per_page": 5,
        "prev_page_url": null,
        "to": 2,
        "total": 2
    }
}

How can I filter this collection to return just specified collection?

Gummibeer commented 2 years ago

Hey,

instead of returning the collection/paginator directly you should use an API resource class to explicitly transform your model to JSON. https://laravel.com/docs/9.x/eloquent-resources

solingenn commented 2 years ago

Ok, I'll try that. Thanks.

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