Api: How to remove 'data' from response json?

Created on 1 Jun 2016  路  3Comments  路  Source: dingo/api

I've searched here and in fractal docs and i found that i need to replace DataArraySerializer with ArraySerializer

$manager->setSerializer(new ArraySerializer());

but i have no idea where to put this in Laravel. I tried putting

    $fractal = app(Manager::class);
    $fractal->setSerializer(new DataArraySerializer);

in controller's construct but it doesn't change anything. Any ideas?

Most helpful comment

In a service provider...

$this->app['Dingo\Api\Transformer\Factory']->setAdapter(function ($app) {
    $manager = new League\Fractal\Manager;
    $manager->setSerializer(new ArraySerializer);

    return new Dingo\Api\Transformer\Adapter\Fractal($manager, 'include', ',');
});

All 3 comments

I just had this question myself and wound up finding an answer that worked: https://joshhornby.co.uk/blog/laravel-dingo-json-api-spec

You'll basically create a new ServiceProvider file, within your App/Providers folder (or wherever you'd normally prefer), and then include this within your config/app.php file's 'providers' array (the same way they include the AppServiceProvider::class etc).

That's all you have to do to globally change the default serializer used. Of course, you'll have to make modifications to the code provided on the above URL, as you want to use ArraySerializer instead.

In a service provider...

$this->app['Dingo\Api\Transformer\Factory']->setAdapter(function ($app) {
    $manager = new League\Fractal\Manager;
    $manager->setSerializer(new ArraySerializer);

    return new Dingo\Api\Transformer\Adapter\Fractal($manager, 'include', ',');
});

It didn't work for me, when I user includeDefaults inside the transformer, It appends data key. :-(

Was this page helpful?
0 / 5 - 0 ratings

Related issues

cristiammercado picture cristiammercado  路  3Comments

HTMHell picture HTMHell  路  4Comments

BartHuis picture BartHuis  路  3Comments

jhayiwg picture jhayiwg  路  3Comments

MicroDroid picture MicroDroid  路  3Comments