简体   繁体   中英

Dingo API transformer

I have below transformer which works but there is unnecessary array in the response.

class NamedayTransformer extends TransformerAbstract
{
    public function transform(Nameday $nameday)
    {
        return [
            'nameday' => $nameday->name,
            'day' => $nameday->day,
            'month' => $nameday->month,
        ];
    }
}

This is the response

{
    "data": [
        {
            "nameday": "František",
            "day": 4,
            "month": 10
        }
    ]
}

I would prefer to have simple json instead of array IN json. Could you please recommend a solution?

This happens when you use get() which returns a collection. That is possible array of objects. So it's not dependent on Dingo API.

If the data you want to return is supposed to be just one object, you can try first()

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM