1

I already looked into several example on how to remove the key "data" in response, but i cannot fix it.

I tried to use the callback provided by dingo

return $this->collection($users, new UserTransformer, function ($resource, $fractal) {
    $fractal->setSerializer(new ArraySerializer);
});

In change the "$fractal->setSerializer(new CustomSerializer);" to "$fractal->setSerializer(new ArraySerializer);" since I dont have CustomSerializer(And how to make this custom serializer?) based on the fractal documentation array serializer but the out put has the key "data".

I also tested the fractal library in different project and implementing the ArraySerializer as the Serializer, and it works.

What am I missing in the setup of dingo-fractal?

UPDATE* I included the setup in the config

$this->app->bind('Dingo\Api\Transformer\Adapter\Fractal', function($app) {
        $fractal = $app->make('\League\Fractal\Manager');
        $serializer = new \League\Fractal\Serializer\ArraySerializer();

        $fractal->setSerializer($serializer);
        return new \Dingo\Api\Transformer\Adapter\Fractal($fractal);
    });

And in my controller

    $obj = \App\EloquentModel\User::find(1);
    return $this->response->item($obj, new UserTransformer);

And in my UserTransformer

public function transform(User $trans)
{
    return [
        'id'            => (int) $trans->id,
        'name'          => $trans->name,
        'description'   => $trans->description
    ];
}

Applying those things removed the key "data" for single item.

{
   "id": 1,
   "name": "Juan",
   "description": "The BOss"
}

But when I try to make it an Array. User::all(), the response has the "data" key.

Thanks.

Mike Andrews
  • 3,045
  • 18
  • 28
chkm8
  • 1,302
  • 1
  • 13
  • 34
  • I also tried this> $this->app->bind('Dingo\Api\Transformer\Adapter\Fractal', function($app) { $fractal = $app->make('\League\Fractal\Manager'); $serializer = new \League\Fractal\Serializer\ArraySerializer; $fractal->setSerializer($serializer); return new \Dingo\Api\Transformer\Adapter\Fractal($fractal); }); – chkm8 Jun 20 '16 at 11:17

1 Answers1

0

I was missing the CustomSerializer, a class that is extend by ArraySerializer. Thanks to this link enter link description here

Community
  • 1
  • 1
chkm8
  • 1,302
  • 1
  • 13
  • 34