dingo / api

A RESTful API package for the Laravel and Lumen frameworks.
BSD 3-Clause "New" or "Revised" License
9.33k stars 1.25k forks source link

laravel5.3 transform use question #1423

Open youmikuang opened 7 years ago

youmikuang commented 7 years ago

I used laravel5.3 and has a question

return $this->collection($project, new ProjectTransformer());

// i get data
{
    "data": [
        {
            "title": "品牌介绍",
            "agency_id": 253,
            "rank": 2,
            "type": "系统",
            "en_title": "brand-introduction"
        },
        {
            "title": "成果展示",
            "agency_id": 253,
            "rank": 4,
            "type": "系统",
            "en_title": "demonstration-of-results"
        }
    ]
}

I want to get this data and Piece together a new array,

{
    "data": [
        {
            "title": "品牌介绍",
            "agency_id": 253,
            "rank": 2,
            "type": "系统",
            "en_title": "brand-introduction"
        },
        {
            "title": "成果展示",
            "agency_id": 253,
            "rank": 4,
            "type": "系统",
            "en_title": "demonstration-of-results"
        }
    ],
   "status"  : 1
}

how can i do that

catalinux commented 6 years ago

The way your models are glued in a response is handled by ypur serializer. You should define a new serializer and set it to your fractal manager.

I think right now you are using DataArraySerializer. You could extend that serializer DataArraySerializer and change collection method :


  public function collection($resourceKey, array $data)
    {
        return ['data' => $data];
    }
youmikuang commented 6 years ago

@catalinux thinks i will have a try