Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Filter collection based on specific locale #279

Closed
solingenn opened this issue May 3, 2022 · 3 comments
Closed

Filter collection based on specific locale #279

solingenn opened this issue May 3, 2022 · 3 comments
Assignees

Comments

@solingenn
Copy link

solingenn commented May 3, 2022

``Collection is returning all translations and I don't know how to filter it using laravel-translatable.
I have tried using translation('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
Copy link
Member

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

@Gummibeer Gummibeer self-assigned this May 4, 2022
@solingenn
Copy link
Author

Ok, I'll try that.
Thanks.

@github-actions
Copy link
Contributor

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Development

No branches or pull requests

2 participants