Skip to content

Transformer not working for Collection responses #30

Closed
@thylo

Description

@thylo

Hi,

I could be wrong but it seems that collections responses are not transformed by my Transformer. It do work for single Model returns :

Here is my code

API::transform('User', 'UserTransformer');
Route::api(['version' => 'v1','prefix'=>'api', 'protected' => true], function()
{
    Route::get('me', function(){
        return API::user();
    });
    Route::get('users', function()
    {
        return User::all();
    });
    Route::resource('room-types', 'Admin\Controllers\Api\RoomTypesController');
});

returns for /api/me is correct (name parameter is correct) :

{
    "data": {
        "id": 3,
        "name": "Julien Moreau",
        "email": "admin@example.com",
        "activated": true
    }
}

meanwhile return for /api/users is not correct

{
    "users": [
        {
            "id": 1,
            "email": "admin@admin.com",
            "last_login": "2014-04-30 14:28:03",
            "first_name": "Admin",
            "last_name": null,
            "updated_at": "2014-04-30 14:28:03"
        },
        {
            "id": 2,
            "email": "user@user.com",
            "last_login": null,
            "first_name": null,
            "last_name": null,
            "updated_at": "2014-04-30 14:24:00"
        },
        {
            "id": 3,
            "email": "admin@example.com",
            "last_login": "2014-05-01 08:02:54",
            "first_name": "Julien",
            "last_name": "Moreau",
            "updated_at": "2014-05-01 08:02:54"
        }
    ]
}

The "testTransformingCollectionUsingTransformerClassName" test from "TransformerTest.php" seems to ensure that it works.

Other question, why is the key param for single models "data" and not "user" as it should be?

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions