mercredi 22 janvier 2020

Laravel API Resource Collection return specific fields from other resource

I am working on an api that will be accessible via a mobile app.

I have defined resources and collections for respective endpoints. My problem is now is that I want to return different api json data based on what ever collection.

Here is an example

Provinces has cities and suburbs, so in json format I need to have

    "data": [
        {
            "id": 1,
            "name": "Eastern Cape",
            "cities": [
                {
                    "name": "Alice"
                },
            ],
            "suburbs": [
                    "name": "Suburb 1"
            ]
        },
]

I want different data when the cities resource is called in news api collection

    "data": [
        {
            "id": 1,
            "name": "Eastern Cape",
            "cities": [
                {
                    "name": "Alice",
                    "municipality": "municipality name",
                },
            ],
            "suburbs": [
                    "name": "Suburb 1",
                    "ward_number": "ward 1"
            ]
        },
]


via Chebli Mohamed

Aucun commentaire:

Enregistrer un commentaire